The Windows Forms Application Project Type

Windows Forms Application projects consist of a default form class and, in the case of C#, a default static Program class. After creating the project, you are immediately presented with a blank, default form opened in the Windows Forms Designer. For a refresher on the basic capabilities and components of the Windows Forms Designer, see Chapter 6, “Introducing the Editors and Designers.”

Setting the Startup Form

Although the default project creates only a single form, you can add multiple forms at any time. This then raises the question of how to indicate at design time which form you initially want displayed at runtime (if any). There are two methods:

Image For Visual Basic projects, the startup form is set using the Project Properties dialog box. The Startup Object drop-down in this dialog box contains a list of all valid form objects. You simply select the form you want launched on startup, and you’re all set.

Image For Visual C# projects, a slightly more complex approach is needed. The notion of a C# startup object is simply any class that implements a Main() method. Within the body of the Main method, you need to place a line of code that passes in a form instance to the Application.Run method, like this: Application.Run(new OrderForm()). Assuming that you have a class that implements Main and code that calls Application.Run in that Main method, you can then select the specific startup object via the Project Properties dialog box. The Program class, which is created for you during the project creation process, already implements the Main method and by default runs the default form (Form1) on startup.

Inheriting Another Form’s Appearance

If your form looks similar to another form that you have already developed, you have the option of visually inheriting that other form’s appearance. Visual Studio provides an Inherited Form project item template to help you along this path.

To create a form that visually inherits another, select Project, Add New Item. In the Add New Item dialog box, select the Inherited Form item type (located under Visual C# Items, under the Windows Forms category). The Inheritance Picker dialog box then lists the available forms within the current project that you can inherit from. Note that you also have the option of manually browsing to an existing assembly if you want to inherit from a form that doesn’t appear in the list. After you select the base form, Visual Studio creates the new form class; its code already reflects the base class derivation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset