C# Simplified, part 6: Working with Windows Applications - Code Analysis
(Page 3 of 4 )
Lines 3 and 4 refer to the required namespaces to be used in this program. Note that a new namespace, as discussed in one of the previous sections, have been called. This namespace exposes the Form class and its functionalities. Line 6 specifies the class declaration that extends the Form class.
Line 9 calls the constructor, which will be of course the same as the class name. You can perform whatever task you want, such as creation of controls like Buttons, TextBoxes and so forth inside the constructor.
Lines 10 and 12 refer to the beginning and ending of class constructors by using opening and closing curly braces. Line 14 specifies the Main method, which is required in all programs. As discussed in one of the previous articles, it is the entry point for every C# application. Finally, in line 16 an instance of the class HelloWorldWin1 is created as an argument to the run() method of the Application class.
You can modify the above listing by adding additional statements inside the constructor. The relevant explanations are shown as comments
/*Changes the forms location
Location is the property of the Form class
The keyword "this" refers to the current instance of the Form
*/
this.Location = new System.Drawing.Point(300,300);
If you are calling the System.Drawing namespace (Using System.Drawing) declaration on the top of the program, then the code given below is equivalent to the above listing:
this.Location = new Point(300,300);
The keyword "this" can be applied only to Forms instance and not to other controls |
/* Changes the Forms Title
Text is the property of the class Form
*/
this.Text = "Welcome to Windows Forms"
/* Changes the mouse cursor
when you move your mouse inside the Form
*/
this.Cursor = Cursors.Hand
/* Changes the background color of the Form
Backcolor is the property of the Form class inside the
System.Drawing namespace
*/
this.Backcolor = System.Drawing .Color.Orange
/* Sets the Forms position upon loading
The Form will appear on the center of the screen
*/
this.Startposition = FormStartPosition.CenterScreen
/* Modifies the Forms size
The first parameter denotes width and the second denotes height
*/
this.size = new Size(400,200)
Listing 6.3 shows the modified version of the previous listing after applying certain properties mentioned in this section:
Listing 6.3
001: // HelloWorldWin2.cs
002: // -----------------
003: using System;
004: using System.Windows.Forms;
005: using System.Drawing;
006:
007: public class HelloWorldWin2:Form
008: {
009:
010: public HelloWorldWin2()
011: {
012: //Forms title
013: this.Text = "Welcome to Windows Forms";
014: this.Size = new Size(400,200);
015: this.Cursor = Cursors.Hand;
016: }
017:
018: public static void Main()
019: {
020: Application.Run(new HelloWorldWin2());
021: }
022: }
The output of the above code upon execution will look like Figure 6.2.

Figure 6.2
As explained in the beginning, the .NET Framework provides a lot of controls for developing GUI based applications. Before proceeding further, you should know how to apply these controls in your WinForm application. You can easily add controls to the Form by using the Control class of System.Windows.Forms namespace. The steps required for adding a WinForm control are described below.
First, you must create an instance of the control. For example, if you are adding a TextBox and a Button to the Form, you have to create an object (Instance) of the TextBox and Button class as in listing 6.4:
Listing 6.4
TextBox txt1 = new TextBox();
Button btn1 = new Button();
As the next step, you must apply the necessary properties of the TextBox and the Button control using the above objects txt1 and btn1. The properties can be that of Location, Width, Height, BackColor, and so on. It is recommended that you give a meaningful name for the object so that you can easily debug the code later.
Finally, you must add the object to the Forms control collection. For this purpose, you should make use of the Add() method of the Control class as shown in listing 6.5:
Listing 6.5
this.Controls.Add(txt1);
this.Controls.Add(btn1);
In the above code, the keyword this signifies the current instance of the Form.
That's all there is to it. You have learned how to add controls to the Form. In the forthcoming sections, you will learn more about each of these controls in detail.
Next: Working with WinForm controls using Visual Studio .NET >>
More C# Articles
More By Anand Narayanaswamy