using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using StructuredExceptionHandlingComponentCS ;

namespace StructuredExceptionHandlingClientCS
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button button4;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.CheckBox checkBox1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.button4 = new System.Windows.Forms.Button();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.checkBox1 = new System.Windows.Forms.CheckBox();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(24, 80);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(104, 48);
			this.button1.TabIndex = 0;
			this.button1.Text = "Open File Without Exception Handler";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(24, 152);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(104, 48);
			this.button2.TabIndex = 1;
			this.button2.Text = "Open File With Exception Handler";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(272, 80);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(168, 48);
			this.button3.TabIndex = 2;
			this.button3.Text = "Call Private Exception-Throwing Method Directly ";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(272, 144);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(168, 48);
			this.button4.TabIndex = 3;
			this.button4.Text = "Call Private Exception-Throwing Method Indirectly";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(24, 40);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(152, 20);
			this.textBox1.TabIndex = 4;
			this.textBox1.Text = "C:\\NonExistentFile";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(24, 16);
			this.label1.Name = "label1";
			this.label1.TabIndex = 5;
			this.label1.Text = "File to Open:";
			// 
			// checkBox1
			// 
			this.checkBox1.Checked = true;
			this.checkBox1.CheckState = System.Windows.Forms.CheckState.Checked;
			this.checkBox1.Location = new System.Drawing.Point(272, 48);
			this.checkBox1.Name = "checkBox1";
			this.checkBox1.Size = new System.Drawing.Size(240, 24);
			this.checkBox1.TabIndex = 6;
			this.checkBox1.Text = "Throw Private Exception When Called";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(520, 230);
			this.Controls.Add(this.checkBox1);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.button4);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Name = "Form1";
			this.Text = "Structured Exception Handling Sample C#";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			try
			{
				Application.Run(new Form1());
			}
			catch (Exception x)
			{
				MessageBox.Show  ("Caught unhandled exception " + x.ToString ( ) ) ;

			}
		}

		// User clicked button for unhandled system exception.
		// Call a function we know will fail with an exception, in 
		// this case, opening a file we know doesn't exist.

		private void button1_Click(object sender, System.EventArgs e)
		{
	        System.IO.FileStream foo ;
			foo = System.IO.File.Open(textBox1.Text, System.IO.FileMode.Open) ;
		}

		// User clicked button for a handled system exception.
		// Call a function we know will fail with an exception, in 
		// this case, opening a file we know doesn't exist. Same as
		// Previous example, only this time we do it in a try-catch
		// block to handle the exception it produces. 

		private void button2_Click(object sender, System.EventArgs e)
		{
			try
			{

				// Perform an operation that we know will cause an exception

				System.IO.FileStream foo ;
				foo = System.IO.File.Open(textBox1.Text, System.IO.FileMode.Open) ;
			}

				// This catch block handles the FileNotFoundException
				// In this case, I just show a message box for you to see. 

			
					catch (System.IO.FileNotFoundException fnfe)
					{
						MessageBox.Show ( fnfe.ToString( ), 
						"FileNotFoundException caught") ;
					}

				// This catch block handles the DirectoryNotFoundException.
				// It offers you the chance to re-throw the exception. 


						catch (System.IO.DirectoryNotFoundException  x)
						{
							if (MessageBox.Show (x.ToString( ), "Other type of exception caught, rethrow?", 
								MessageBoxButtons.YesNo) == DialogResult.Yes)
							{
								throw x ;
							}
						}
						
			catch (Exception x)
			{
				MessageBox.Show (x.Message) ;
			}
		}

		// User clicked Private Single-level exception. Create an object
		// and call a method on it that we know will throw an exception.
		// Catch exception and report it to the user.

		private void button3_Click(object sender, System.EventArgs e)
		{
			try
			{
				StructuredExceptionHandlingComponentCS.Class1 foo = new StructuredExceptionHandlingComponentCS.Class1() ;
				foo.BottomFunction(checkBox1.Checked) ;
			}
			catch (MyOwnException x)
			{
				MessageBox.Show("MyOwnException caught at: " + 
					x.ExceptionTime.ToLongTimeString ( )) ;
			}
		}

		// User clicked Private Multi-level exception. Create an object
		// and call a method on it that we know will throw an exception. The
		// object uses a try-finally handler internally to demonstrate that feature.
		// Catch exception and report it to the user.

		private void button4_Click(object sender, System.EventArgs e)
		{
			try
			{
				StructuredExceptionHandlingComponentCS.Class1 foo = new StructuredExceptionHandlingComponentCS.Class1() ;
				foo.MiddleFunction(checkBox1.Checked) ;
			}
			catch (MyOwnException x)
			{
				MessageBox.Show("MyOwnException caught at: " + 
					x.ExceptionTime.ToLongTimeString ( )) ;
			}
		}
	}
}
