Tutorial: Working with Windows Forms

This page contains a number of C# codes for the Amateur/Beginners in the Visual C# and .Net PlatForm Environment.

This tutorial will take anyone from the scratch to a good Windows Form creator  although I am still learning at my spare time.

Source Code

  1. //MyForm1.cs  
  2. //This Tutorial will Teach you how to create a Form without Caption Heading  
  3. using System;  
  4. using System.Drawing;  
  5. using System.WinForms;  
  6. class MyForm : System.WinForms.Form  
  7. {  
  8.     public MyForm()  
  9.     {  
  10.     }  
  11.     public static void Main()  
  12.     {  
  13.         Application.Run(new MyForm());  
  14.     }  
  15. }  
  16. /* 
  17. To Compile make a batch File in Dos Mode as compile.bat 
  18. csc /r:System.dll /r:System.Drawing.dll /r:System.WinForms.dll 
  19. /r:Microsoft.Win32.InterOp.dll /out:MyForm1.exe MyForm1.cs 
  20. */  
Output

Source Code

  1. //MyForm2.cs  
  2. //This Tutorial will Teach you how to create a Form with Caption Heading  
  3. using System;  
  4. using System.Drawing;  
  5. using System.WinForms;  
  6. class MyForm : System.WinForms.Form  
  7. {  
  8.     public MyForm()  
  9.     {  
  10.         //Text to be Displayed in the Caption-Title Bar  
  11.         this.Text = "Form Tutorial No.2 From JAYANT";  
  12.     }  
  13.     public static void Main()  
  14.     {  
  15.         Application.Run(new MyForm());  
  16.     }  
  17. }  
  18. /* 
  19. To Compile make a batch File in Dos Mode as compile.bat 
  20. csc /r:System.dll /r:System.Drawing.dll /r:System.WinForms.dll 
  21. /r:Microsoft.Win32.InterOp.dll /out:MyForm2.exe MyForm2.cs 
  22. */  
Output

Source Code

  1. //MyForm3.cs  
  2. //This Tutorial will Teach you how to create a Form with Added Functionality describing Size  
  3. using System;  
  4. using System.Drawing;  
  5. using System.WinForms;  
  6. class MyForm : System.WinForms.Form  
  7. {  
  8.     public MyForm()  
  9.     {  
  10.         //Text to be Displayed in the Caption-Title Bar  
  11.         this.Text = "Form Tutorial No.3 From JAYANT";  
  12.         this.StartPosition = FormStartPosition.CenterScreen;  
  13.         this.AutoScaleBaseSize = new Size(5, 13);  
  14.         this.ClientSize = new Size(400, 200); //Size except the Title Bar-CaptionHeight  
  15.         this.MinTrackSize = new Size(400, (200 + SystemInformation.CaptionHeight));  
  16.         this.MaximizeBox = false;  
  17.     }  
  18.     public static void Main()  
  19.     {  
  20.         Application.Run(new MyForm());  
  21.     }  
  22. }  
  23. /* 
  24. To Compile make a batch File in Dos Mode as compile.bat 
  25. csc /r:System.dll /r:System.Drawing.dll /r:System.WinForms.dll 
  26. /r:Microsoft.Win32.InterOp.dll /out:MyForm3.exe MyForm3.cs 
  27. */  
Output

 

Source Code

  1. //MyForm4.cs  
  2. //This Tutorial will Teach you how to create a Form with Label on the Form  
  3. using System;  
  4. using System.Drawing;  
  5. using System.WinForms;  
  6. class MyForm : System.WinForms.Form  
  7. {  
  8.     Label label1;  
  9.     public MyForm()  
  10.     {  
  11.         label1 = new Label();  
  12.         label1.UseMnemonic = true;  
  13.         label1.Text = "First &Name:";  
  14.         label1.Location = new Point(15, 15);  
  15.         label1.BackColor = Color.Pink;  
  16.         label1.ForeColor = Color.Maroon;  
  17.         label1.BorderStyle = System.WinForms.BorderStyle.FixedSingle;  
  18.         label1.Size = new Size(label1.PreferredWidth, label1.PreferredHeight + 2);  
  19.         //Text to be Displayed in the Caption-Title Bar  
  20.         this.Text = "Form Tutorial No.4 From JAYANT";  
  21.         this.StartPosition = FormStartPosition.CenterScreen;  
  22.         this.AutoScaleBaseSize = new Size(5, 13);  
  23.         this.ClientSize = new Size(300, 200); //Size except the Title Bar-CaptionHeight  
  24.         this.MinTrackSize = new Size(300, (200 + SystemInformation.CaptionHeight));  
  25.         this.AutoScroll = true;  
  26.         this.MaximizeBox = false;  
  27.         this.Controls.Add(label1);  
  28.     }  
  29.     public static void Main()  
  30.     {  
  31.         Application.Run(new MyForm());  
  32.     }  
  33. }  
  34. /* 
  35. To Compile make a batch File in Dos Mode as compile.bat 
  36. csc /r:System.dll /r:System.Drawing.dll /r:System.WinForms.dll 
  37. /r:Microsoft.Win32.InterOp.dll /out:MyForm4.exe MyForm4.cs 
  38. */  
Output

Source Code

  1. //MyForm5.cs  
  2. //This Tutorial will Teach Mouse clicking Events and MessageBox(without Title_Heading) calling  
  3. using System;  
  4. using System.Drawing;  
  5. using System.WinForms;  
  6. class MyForm : System.WinForms.Form  
  7. {  
  8.     Label label1;  
  9.     public MyForm()  
  10.     {  
  11.         label1 = new Label();  
  12.         label1.UseMnemonic = true;  
  13.         label1.Text = "First &Name:";  
  14.         label1.Location = new Point(15, 15);  
  15.         label1.BackColor = Color.Pink;  
  16.         label1.ForeColor = Color.Maroon;  
  17.         label1.BorderStyle = System.WinForms.BorderStyle.FixedSingle;  
  18.         label1.Size = new Size(label1.PreferredWidth, label1.PreferredHeight + 2);  
  19.         //Text to be Displayed in the Caption-Title Bar  
  20.         this.Text = "Form Tutorial No.5 From JAYANT";  
  21.         this.StartPosition = FormStartPosition.CenterScreen;  
  22.         this.AutoScaleBaseSize = new Size(5, 13);  
  23.         this.ClientSize = new Size(300, 200); //Size except the Title Bar-CaptionHeight  
  24.         this.MinTrackSize = new Size(300, (200 + SystemInformation.CaptionHeight));  
  25.         this.AutoScroll = true;  
  26.         this.MaximizeBox = false;  
  27.         this.Controls.Add(label1);  
  28.         this.Click += new EventHandler(clicking);  
  29.     }  
  30.     public void clicking(object ob, EventArgs e)  
  31.     {  
  32.         MessageBox.Show("You clicked on Form Area");  
  33.     }  
  34.     public static void Main()  
  35.     {  
  36.         Application.Run(new MyForm());  
  37.     }  
  38. }  
  39. /* 
  40. To Compile make a batch File in Dos Mode as compile.bat 
  41. csc /r:System.dll /r:System.Drawing.dll /r:System.WinForms.dll 
  42. /r:Microsoft.Win32.InterOp.dll /out:MyForm5.exe MyForm5.cs 
  43. */  
Output

 

Source Code

  1. //MyForm6.cs  
  2. //This Tutorial will Teach Mouse clicking Events and MessageBox(with Title_Heading) calling  
  3. using System;  
  4. using System.Drawing;  
  5. using System.WinForms;  
  6. class MyForm : System.WinForms.Form  
  7. {  
  8.     Label label1;  
  9.     public MyForm()  
  10.     {  
  11.         label1 = new Label();  
  12.         label1.UseMnemonic = true;  
  13.         label1.Text = "First &Name:";  
  14.         label1.Location = new Point(15, 15);  
  15.         label1.BackColor = Color.Pink;  
  16.         label1.ForeColor = Color.Maroon;  
  17.         label1.BorderStyle = System.WinForms.BorderStyle.FixedSingle;  
  18.         label1.Size = new Size(label1.PreferredWidth, label1.PreferredHeight + 2);  
  19.         //Text to be Displayed in the Caption-Title Bar  
  20.         this.Text = "Form Tutorial No.6 From JAYANT";  
  21.         this.StartPosition = FormStartPosition.CenterScreen;  
  22.         his.AutoScaleBaseSize = new Size(5, 13);  
  23.         this.ClientSize = new Size(300, 200); //Size except the Title Bar-CaptionHeight  
  24.         this.MinTrackSize = new Size(300, (200 + SystemInformation.CaptionHeight));  
  25.         this.AutoScroll = true;  
  26.         this.MaximizeBox = false;  
  27.         this.Controls.Add(label1);  
  28.         this.Click += new EventHandler(clicking);  
  29.     }  
  30.     public void clicking(object ob, EventArgs e)  
  31.     {  
  32.         MessageBox.Show("You clicked on Form Area""Title_JAYANT");  
  33.     }  
  34.     public static void Main()  
  35.     {  
  36.         Application.Run(new MyForm());  
  37.     }  
  38. }  
  39. /* 
  40. To Compile make a batch File in Dos Mode as compile.bat 
  41. csc /r:System.dll /r:System.Drawing.dll /r:System.WinForms.dll 
  42. /r:Microsoft.Win32.InterOp.dll /out:MyForm6.exe MyForm6.cs 
  43. */  
Output

Source Code

  1. //MyForm7.cs  
  2. //This Tutorial will Teach Mouse clicking Events and changing the Form colour  
  3. using System;  
  4. using System.Drawing;  
  5. using System.WinForms;  
  6. class MyForm : System.WinForms.Form  
  7. {  
  8.     Label label1;  
  9.     public MyForm()  
  10.     {  
  11.         label1 = new Label();  
  12.         label1.UseMnemonic = true;  
  13.         label1.Text = "First &Name:";  
  14.         label1.Location = new Point(15, 15);  
  15.         label1.BackColor = Color.Pink;  
  16.         label1.ForeColor = Color.Maroon;  
  17.         label1.BorderStyle = System.WinForms.BorderStyle.FixedSingle;  
  18.         label1.Size = new Size(label1.PreferredWidth, label1.PreferredHeight + 2);  
  19.         //Text to be Displayed in the Caption-Title Bar  
  20.         this.Text = "Form Tutorial No.7 From JAYANT";  
  21.         this.BackColor = Color.BurlyWood;  
  22.         this.StartPosition = FormStartPosition.CenterScreen;  
  23.         this.AutoScaleBaseSize = new Size(5, 13);  
  24.         this.ClientSize = new Size(300, 200); //Size except the Title Bar-CaptionHeight  
  25.         this.MinTrackSize = new Size(300, (200 + SystemInformation.CaptionHeight));  
  26.         this.AutoScroll = true;  
  27.         this.MaximizeBox = false;  
  28.         this.Controls.Add(label1);  
  29.         this.Click += new EventHandler(clicking);  
  30.     }  
  31.     public void clicking(object ob, EventArgs e)  
  32.     {  
  33.         MessageBox.Show("Click will change the Form Color""Title_JAYANT");  
  34.         this.BackColor = Color.Red;  
  35.     }  
  36.     public static void Main()  
  37.     {  
  38.         Application.Run(new MyForm());  
  39.     }  
  40. }  
  41. /* 
  42. To Compile make a batch File in Dos Mode as compile.bat 
  43. csc /r:System.dll /r:System.Drawing.dll /r:System.WinForms.dll 
  44. /r:Microsoft.Win32.InterOp.dll /out:MyForm7.exe MyForm7.cs 
  45. */  
Output

Source Code

  1. //MyForm8.cs  
  2. //This Tutorial will Teach Mouse clicking Events and  
  3. using System;  
  4. using System.Drawing;  
  5. using System.WinForms;  
  6. class MyForm : System.WinForms.Form  
  7. {  
  8.     Label label1;  
  9.     TextBox txtbx1;  
  10.     Button btn1;  
  11.     Button exit;  
  12.     public MyForm()  
  13.     {  
  14.         label1 = new Label();  
  15.         txtbx1 = new TextBox();  
  16.         btn1 = new Button();  
  17.         exit = new Button();  
  18.         label1.UseMnemonic = true;  
  19.         label1.Text = "First &Name:";  
  20.         label1.Location = new Point(15, 15);  
  21.         label1.BackColor = Color.Pink;  
  22.         label1.ForeColor = Color.Maroon;  
  23.         label1.BorderStyle = System.WinForms.BorderStyle.FixedSingle;  
  24.         label1.Size = new Size(label1.PreferredWidth, label1.PreferredHeight + 2);  
  25.         txtbx1.Text = "Enter Your Name";  
  26.         txtbx1.Location = new Point(15 + label1.PreferredWidth + 5, 15);  
  27.         txtbx1.BorderStyle = System.WinForms.BorderStyle.FixedSingle;  
  28.         txtbx1.BackColor = Color.LightGray;  
  29.         txtbx1.ForeColor = Color.Maroon;  
  30.         txtbx1.Size = new Size(90, 20);  
  31.         btn1.Text = "&OK";  
  32.         btn1.Location = new Point(15 + txtbx1.Location.X + txtbx1.Size.Width, 15);  
  33.         btn1.Size = new Size(50, 20);  
  34.         exit.Text = "Exit";  
  35.         exit.Location = new Point(150, 150);  
  36.         exit.Size = new Size(90, 20);  
  37.         exit.BackColor = Color.Maroon;  
  38.         exit.ForeColor = Color.White;  
  39.         //Text to be Displayed in the Caption-Title Bar  
  40.         this.Text = "Form Tutorial No.8 From JAYANT";  
  41.         this.StartPosition = FormStartPosition.CenterScreen;  
  42.         this.AutoScaleBaseSize = new Size(5, 13);  
  43.         this.ClientSize = new Size(300, 200); //Size except the Title Bar-CaptionHeight  
  44.         this.MinTrackSize = new Size(300, (200 + SystemInformation.CaptionHeight));  
  45.         this.AutoScroll = true;  
  46.         this.MaximizeBox = false;  
  47.         this.Controls.Add(label1);  
  48.         this.Controls.Add(txtbx1);  
  49.         this.Controls.Add(btn1);  
  50.         this.Controls.Add(exit);  
  51.         btn1.Click += new EventHandler(Btn_Clicked);  
  52.         exit.Click += new EventHandler(Ext_Clicked);  
  53.     }  
  54.     public void Btn_Clicked(object ob, EventArgs e)  
  55.     {  
  56.         if (txtbx1.Text == "Enter Your Name")  
  57.             MessageBox.Show("You Have'nt Entered Your Name""Title_JAYANT");  
  58.         else  
  59.             MessageBox.Show("Hello!!! " + txtbx1.Text, "Title_JAYANT");  
  60.     }  
  61.     public void Ext_Clicked(object ob, EventArgs e)  
  62.     {  
  63.         Application.Exit();  
  64.         MessageBox.Show("Successfully Closed""EXIT");//not Shown! Do you know Why?  
  65.     }  
  66.     public static void Main()  
  67.     {  
  68.         Application.Run(new MyForm());  
  69.     }  
  70. }  
  71. /* 
  72. To Compile make a batch File in Dos Mode as compile.bat 
  73. csc /r:System.dll /r:System.Drawing.dll /r:System.WinForms.dll 
  74. /r:Microsoft.Win32.InterOp.dll /out:MyForm8.exe MyForm8.cs 
  75. */  
Output