Creating a Form


Although there are many different ways of approaching form design, the starting point for all of them within Visual Studio is the Windows Application project template. From the New Project dialog box, select this template, give the project an appropriate name, and click OK (see Figure 14.2).

Figure 14.2. Creating a new Windows forms project.


The Windows Application Project Type

Windows 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, reference Chapter 6, "Introducing the Editors and Designers."

Setting the Startup Form

Although the default project creates only a single form, you can, of course, 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:

  • 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.

  • 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.

Inheriting Another Form's Appearance

If your form will look 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. 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 will create the new form class; its code will already reflect the base class derivation.

Form Properties and Events

A form is like any other control: You can use the Properties window in the IDE to control its various properties. Although we won't touch on all of them here, you will want to consider a few key properties as you begin your form design process.

Startup Location

You use the form's StartPosition property to place the form's window on the screen when it is first displayed. This property accepts a FormStartPosition enumeration value; the possible settings are documented in Table 14.1.

Table 14.1. FormStartPosition Enumeration Values

Value

Description

CenterParent

Centers the form within its parent form.

CenterScreen

Centers the form within the current display screen.

Manual

The form will position itself according to the Form.Location property value.

WindowsDefaultBounds

Positions the form at the Windows default location; the form's bounds are determined by the Windows default as well.

WindowsDefaultLocation

Positions the form at the Windows default location; the form's size is determined by the Form.Size property (this is the default setting).


Appearance

Given our discussion on the priority of UI design, it should come as no surprise that the appearance of the form is an important part of the overall application's user experience.

For the most part, the default appearance property values are sufficient for the typical application. You should set the ForeColor and BackColor properties according to the color scheme identified for your application. Note that when you add controls to the form, most of them have their own individual ForeColor values set to mimic that of the form.

Some properties allow you to implement a more extravagant user interface. The Opacity property allows you to implement transparent or semitransparent forms. This capability might be useful for situations in which users want to see a portion of the screen that actually sits behind the form's window. In addition to the Opacity property, you use the Form.BackgroundImage property to set an image as the form's background. This property is best used to display subtle color gradients or graphics not possible with just the BackColor property.

Keeping in mind our goal of rapidly crafting the form, most of the activities within the designer described in this chapter consist of tweaking the form's properties and adding controls from the Toolbox to the form.

Form Events

Forms inherit the same event-driven architecture as other controls do. Certain public events defined on the Form class are useful as injection points across the continuum of a form's life.

Figure 14.3 shows the various stages (and corresponding events) from form inception to close. To react to a form event, you first need to create an event handler.

Figure 14.3. The events in the life of a Windows form.


Creating an Event Handler

Visual Studio's Properties window provides a speedy mechanism for defining an event handler. First, select the form of interest. Then click on the Events button in the Properties window's toolbar. The window will now show a list of every event defined on the form. Double-clicking on the event will create a blank event handler routine and open it in the code editor for you. The event handler will have the correct arguments list and will follow established standards for event handler naming (typically, object_eventname).

Figure 14.4 depicts the form events within the Properties window.

Figure 14.4. Form events in the Properties window.


With the form in place, you can start placing controls onto its surface.




Microsoft Visual Studio 2005 Unleashed
Microsoft Visual Studio 2005 Unleashed
ISBN: 0672328194
EAN: 2147483647
Year: 2006
Pages: 195

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net