First, you need to create a Windows Forms application. For the following example, create a blank form and show it on the screen, This example does not use Visual Studio .NET It has been entered in a text editor and compiled using the command-line compiler.
public class MyForm System.Windows.Forms.Form
static void Mainl)
When you compile and run this example, you will get a small blank form without a caption, Not real functional, but it is a Windows Form, As you look at the code, two items deserve attention. The first is the fact that you have used inheritance to create the MyFormclass. The following line declares that MyFormis derived from System.
Windows Forms public class MyForm: System.Windows.Forms.Form
The Form class is one of the main classes in the Systerm. Windows. Forms namespace. The other section of code that you want to look at is:
static void Main()
Main is the default entry point into any C# client application. Typically in larger applications, the Main method would not be in a form, but in a class that is responsible for any startup processing that needs to be done, In this case, you would set the startup class name in the project properties dialog box, Notice the attribute [STAThreadl. This sets the COM threading model to single-threaded apartment (STA), The STAthreading model is required for COM interop and is added by default to a Windows Form project.
The Applica tion. Run() method is responsible for starting the standard application message loop, ApplicationRun () has three overloads: the first takes no parameter, the second takes an ApplicationContext object as a parameter, and the one you see in the example takes a form object as a parameter, In the example, the MyFormobject will become the main form of the application. This means that when this form is closed, the application ends. By using the ApplicationContext class, you can gain a little more control over when the main message loop ends and the application exits.
The Application class contains some very useful functionality. It provides a handful of static methods and properties for controlling the application’s starting and stopping process and to gain access to the Windows messages that are being processed by the application. The following table lists some of
the more useful of these methods and properties.
Now, what does this sample application look like when it is generated in Visual Studio 2005? The first thing to notice is that two files are created because Visual Studio 2008 takes advantage of the partial class feature of the framework and separates all of the Designer-generated code into a separate file, Using the default name of Forml, the two files are Forml. cs and Forml. Designer. cs. Unless you have the Show All Files option checked on the Project menu you won’t see Forml. Designer. cs in Solution Explorer, Following IS the code that Visual Studio generates for the two files, First is Forml . c s:
using System. Data;
using System. Drawing;
using System. Text;
public partial class Forml Form
public Forml ()
The Designer file of a form should rarely be edited directly, The only exception would be if there is any special processing that needs to take place in the Dispose method.
Looking at the code as a whole for this sample application, you can see it is much longer than the simple command-line example. There are several using statements at the start of the class; most are not necessary for this example. There is no penalty for keeping them there. The class Forml is derived from System Windows.
Forms just like the earlier Notepad example, but things start to get different at this point. First, there is this line in the Forml. Designer file:
private System.ComponentModel.IContainer components = null;
In the example, this line of code doesn’t really do anything, When you add a component to a form, you can also add it to the components object, which is a container, The reason for adding to this container has do with disposing of the form. The form class supports the IDisposable interface because it is implemented in the Component class, When a component is added to the components container, the container will make sure that the components are tracked properly and disposed of when the form is disposed of You can see this if you look at the Dispose method in the code:
protected override void Dispose(bool disposing)
if (disposing && (components != null
Here you can see that when the Dispose method is called, the Dispose method of the components object is also called, and because the component object contains the other components, they are also disposed of.
The constructor of the Fonnl class, which is in the FOrrr(. cs file, looks like this:
public Forml ()
Notice the call to InitializeComponent (). InitializeComponent () is located in Forml. Designer, cs and does pretty much what it describes, and that is to initialize any controls that might have been added to the form, It also initializes the form properties.
For this example, InitializeComponent ( )
looks Jil(the following:
private void InitializeComponent()
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Text = ” Forml’ ;. .
As you can see, it is basic initialization code. This method is tied to the Designer in Visual Studio, When you make changes to the form by using the Designer, the changes are reflected in InitializeComponent (), you make any type of code change in InitializeComponent (), the next time you make a change in the Designer, your changes will be lost. InitializeComponent () gets regenerated after each change in the Designer.
If you need to add additional initialization code for the form or controls and components on the form, be sure to add it after InitializeComponent () is called. InitializeComponent () is also responsible for instantiating the controls so any call that references a control prior to InitializeComPonent () will fail with a null reference exception.
To add a control or component to the form, press Ctrl+Alt+X or select Toolbox from the View menu in VisualStudio .NET Forml should be in design mode. Right-click Forml .cs in Solution Explorer and select View Designer from the context menu. Select the Button control and drag it to the form in the Designer.
You can also double-click the control, and it will be added to the form, Do the same with the TextBox control.
Now that you have added a TextBox control and a Button control to the form, initializeComponent () expands to include the following code:
If you look at the first three lines of code in the method, you can see the Button and TextBox controls are instantiated. Notice the names given to the controls, textBox1 and buttonl, Bydefault, the Designer uses the name of the control and adds an integer value to the name. When you add another button, the Designer adds the name button2, and so on. The next line is part of the SuspendLayout and ResumeLayout pair, SuspendLayout () temporarily suspends the layout events that take place when a control is first _ initialized, At the end of the method the ResumeLayout () method is called to set things back to normal a complex fonn with many controls, the InitializeComponent () method can get quite large.
To change a property value of a control, either pressF4 or select Properties Windows from the View menu, The properties window enables you to modify most of the properties for a control or component, When a change is made in the properties window, the initializeComponent () method is rewritten to reflect the new property value. For example, if the Text property is changed to My Button in the properties window, initializeComponent () will contain this code:
this.button1.Location = new System.Drawing.point(77, 137);
this.button1.Name = “button1”;
this.buttonl.Size = new System. Drawing. Size (75. 23);
this.buttonl.Tablndex = 0;
this.buttonl.Text = ‘My Button’;
this.buttonl.UseVisualStyleBackColor = true;
If you are using an editor other than Visual Studio .NET,you will want to include an InitializeComponent () type function in your designs. Keeping all of this initialization code in one spot will help keep the constructor cleaner, not to mention that if you have multiple constructors you can make sure that the initialization code is called from each constructor.
The importance of understanding the hierarchy becomes apparent during the design and construction of custom controls, If your custom control is a derivative of a current control- for example, a text box with some added properties and methods – you will want to inherit from the text box control and then override and add the properties and methods to suit your needs. However, if you are creating a control that doesn’t match up to any of the controls included with the .NETFramework, you will have to inherit from one of the three base control classes – Control or ScrollableControl if you need autoscrolling capabilities, and ContainerControl if your control needs to be a container of other controls.