Monday, March 31, 2014

Development of Form

Development of Form

It includes with two stages:
  1. Designing
  2. Coding
1) Designing the Form:
  • Drag and Drop the controls from the ToolBox.
  • A Sample Windows Application Project (with Login Form)
Window Login Form Window Form Designing

  • Select the control and set the properties as your wish.
  • For example, set the following properties as per given below:
    1. Form1
      • Text: Login
    2. Label1
      • Text: User Name
    3. Label2
      • Text: Password
    4. TextBox1
      • (No properties are required in this example)
    5. TextBox2
      • PasswordChar: *
    6. Button1
      • Text: OK
    7. Button2
      • Text: Cancel
2) Coding the Form:
  1. Double click on the controls and write the code in the Code window.
  2. For example, double click on OK button and write the following code:
private void button1_Click(object sender, EventArgs e)
   if (textBox1.Text == "system" && textBox2.Text == "manager")
   MessageBox.Show("Login is successful.");
   MessageBox.Show("Invalid Login.");
  • And then, double click on ―Cancel‖ button and write the following code:
private void button2_Click(object sender, EventArgs e)
  • The above process is called as Form coding.
Every Form is a Class:
  1. C#.NET recognizes every form as a class.
  2. In VB 6.0, every form is known as an object for the Form class. But in C#, every form is a class; so that at run time, you can create a ny no. of objects as your wish. For example, if you want to display Login form twice at run time, you can simply create two objects and can show it.
The Automatic Generated Code:
  1. While you design the controls, the Visual Studio generates some automatic code in the “Form1.Designer.cs” file.
  2. To open this file, Open Solution Explorer, Expand Form1, then double click on Form1.Designer.cs.
  3. For example, you can see the automatic generated code for the previous Login example

namespace WindowsFormsApplication1
partial class Form1
/// Required designer variable.
private System.ComponentModel.IContainer components = null;
/// Clean up any resources being used.
/// true if managed resources should be disposed; 
otherwise, false.
protected override void Dispose(bool disposing)
if (disposing && (components != null))
#region Windows Form Designer generated code
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
private void InitializeComponent()
 this.textBox1 = new System.Windows.Forms.TextBox();
 this.textBox2 = new System.Windows.Forms.TextBox();
 this.button1 = new System.Windows.Forms.Button();
 this.button2 = new System.Windows.Forms.Button();
 this.label1 = new System.Windows.Forms.Label();
 this.label2 = new System.Windows.Forms.Label();
// textBox1
 this.textBox1.Location = new System.Drawing.Point(186, 44);
 this.textBox1.Margin = new System.Windows.Forms.Padding(5, 4, 5, 4);
 this.textBox1.Name = "textBox1";
 this.textBox1.Size = new System.Drawing.Size(232, 27);
 this.textBox1.TabIndex = 0;
// textBox2
 this.textBox2.Location = new System.Drawing.Point(186, 102);
 this.textBox2.Margin = new System.Windows.Forms.Padding(5, 4, 5, 4);
 this.textBox2.Name = "textBox2";
 this.textBox2.PasswordChar = '*';
 this.textBox2.Size = new System.Drawing.Size(232, 27);
 this.textBox2.TabIndex = 1;
// button1
 this.button1.Location = new System.Drawing.Point(186, 167);
 this.button1.Margin = new System.Windows.Forms.Padding(5, 4, 5, 4);
 this.button1.Name = "button1";
 this.button1.Size = new System.Drawing.Size(107, 37);
 this.button1.TabIndex = 2;
 this.button1.Text = "OK";
 this.button1.UseVisualStyleBackColor = true;
 this.button1.Click += new System.EventHandler(this.button1_Click); 

// button2
 this.button2.Location = new System.Drawing.Point(314, 167);
 this.button2.Margin = new System.Windows.Forms.Padding(5, 4, 5, 4);
 this.button2.Name = "button2";
 this.button2.Size = new System.Drawing.Size(107, 37);
 this.button2.TabIndex = 3;
 this.button2.Text = "Cancel";
 this.button2.UseVisualStyleBackColor = true;
 this.button2.Click += new System.EventHandler(this.button2_Click);
// label1
 this.label1.AutoSize = true;
 this.label1.Location = new System.Drawing.Point(64, 48);
 this.label1.Margin = new System.Windows.Forms.Padding(5, 0, 5, 0);
 this.label1.Name = "label1";
 this.label1.Size = new System.Drawing.Size(104, 19);
 this.label1.TabIndex = 4;
 this.label1.Text = "User Name:";
// label2
 this.label2.AutoSize = true;
 this.label2.Location = new System.Drawing.Point(64, 107);
 this.label2.Margin = new System.Windows.Forms.Padding(5, 0, 5, 0);
 this.label2.Name = "label2";
 this.label2.Size = new System.Drawing.Size(93, 19);
 this.label2.TabIndex = 5;
 this.label2.Text = "Password:";
// Form1
 this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 19F);
 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
 this.ClientSize = new System.Drawing.Size(492, 244);
 this.Font = new System.Drawing.Font("Tahoma", 12F, 
 System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
 this.Margin = new System.Windows.Forms.Padding(5, 4, 5, 4);
 this.Name = "Form1";
 this.Text = "Login";

private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
  1. Don‘t worry about the above automatic generated code; it will be generated automatically, while you design the form in the Design window.
  2. The entire code is generated in a method called InitializeComponent().
  3. Finally, coming to a conclusion; every form contains two files:
  4. Form1.Designer.cs
  5. Contains the code for designing (Automatically generated code by Visual Studio).
  6. Form1.cs
  7. Contains the actual functionality code (Written by the Programmer).
Class Definition Syntax of a Windows Form

importing section;
namespace ProjectName
 public partial class FormName : Form
   public FormName()
Rules for the Form class definition:
  1. In the importing section, you can import the necessary namespaces that you want.
  2. The namespace name should be same as project name.
  3. A user-defined class with the form name is to be defined.
  4. It should be the sub class of "System.Windows.Forms.Form" class; as it offers some visual design, properties, methods and events for the user-defined form class.
  5. It will be defined as a public class (this is optional). Whenever it is a public class, in future, it can be accessed from other projects also. It should be defined as "partial" class, as its definition is written in the following two files. --> Form1.cs --> Form1.Designer.cs
  6. It should contain a constructor, with a statement called InitializeComponent();, which calls the designer code that is generated in Form1.Designer.cs file.
“Program.cs” file in Windows Forms Applications:
  1. Generally, when you Start the windows application project, automatically the Form1 will be appeared on the screen.
  2. Then don‘t think like directly Form1 will be opened.
  3. In fact, when you click on Start option, the Main() method will be invoked first.
  4. Like Console Applications, Main() method is located in Program.cs file
  5. For reference you open Solution Explorer  ―> Program.cs file.
  6. In the Main() method, you can see two important statements.

This statement enables the better styles (for sleek appearance) for the entire application, based on the current working theme offered by the O/S.

Application.Run(new Form1());

This statement creates a new Form1 class object and that object will be shown on the screen.

No comments:

Post a Comment