Development of Form
It includes with two stages:
- Designing
- Coding
1) Designing the Form:
- Drag and Drop the controls from the ToolBox.
- A Sample Windows Application Project (with Login Form)
- Select the control and set the properties as your wish.
- For example, set the following properties as per given below:
- Form1
- Text: Login
- Label1
- Text: User Name
- Label2
- Text: Password
- TextBox1
- (No properties are required in this example)
- TextBox2
- PasswordChar: *
- Button1
- Text: OK
- Button2
- Text: Cancel
2) Coding the Form:
- Double click on the controls and write the code in the Code window.
- 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."); else MessageBox.Show("Invalid Login."); }
- And then, double click on ―Cancel‖ button and write the following code:
private void button2_Click(object sender, EventArgs e) { Application.Exit(); }
- The above process is called as Form coding.
Every Form is a Class:
- C#.NET recognizes every form as a class.
- 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:
- While you design the controls, the Visual Studio generates some automatic code in the “Form1.Designer.cs” file.
- To open this file, Open Solution Explorer, Expand Form1, then double click on Form1.Designer.cs.
- For example, you can see the automatic generated code for the previous Login example
Default.aspx.cs:
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)) { components.Dispose(); } base.Dispose(disposing); } #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(); this.SuspendLayout(); // // 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.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); 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"; this.ResumeLayout(false); this.PerformLayout(); } #endregion 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; } }
- Don‘t worry about the above automatic generated code; it will be generated automatically, while you design the form in the Design window.
- The entire code is generated in a method called InitializeComponent().
- Finally, coming to a conclusion; every form contains two files:
- Form1.Designer.cs
- Contains the code for designing (Automatically generated code by Visual Studio).
- Form1.cs
- Contains the actual functionality code (Written by the Programmer).
Class Definition Syntax of a Windows Form
//Form1.cs importing section; namespace ProjectName { public partial class FormName : Form { public FormName() { InitializeComponent(); } } }
Rules for the Form class definition:
- In the importing section, you can import the necessary namespaces that you want.
- The namespace name should be same as project name.
- A user-defined class with the form name is to be defined.
- 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.
- 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
- 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:
- Generally, when you Start the windows application project, automatically the Form1 will be appeared on the screen.
- Then don‘t think like directly Form1 will be opened.
- In fact, when you click on Start option, the Main() method will be invoked first.
- Like Console Applications, Main() method is located in Program.cs file
- For reference you open Solution Explorer ―> Program.cs file.
- In the Main() method, you can see two important statements.
Application.EnableVisualStyles();
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