Using Form Properties to Affect Form Behavior

Using Form Properties to Affect Form Behavior

All of the form and control objects that are created using the Visual C# Forms Designer have properties that control their behavior. These properties can be accessed in two ways: either programmatically in your C# application or through the Properties window for the form or control, which is shown in Figure 11-6.

Figure 11-6.
The Visual C# Properties window.

When property values are updated through the Properties window, the Visual C# Forms Designer will generate the necessary code and inject it into your C# source file. There’s no hidden property configuration file that controls the behavior of your forms and controls; instead, your forms and controls are always governed by C# source code.

Setting Border Styles

A variety of border styles can be defined for forms. This is an important property for a form, because the form’s border provides the initial visual cues to the user about the type of interaction possible with the form. For example, if a window appears to allow resizing, the user will expect to be able to resize the window, and your code must be able to render the window properly. The border style is defined by the FormBorderStyle property, which must be set to one of the values from the FormBorderStyle enumeration, listed in Table 11-4.

The following code demonstrates setting the FormBorderStyle property programmatically:

FormBorderStyle = FormBorderStyle.FixedToolWindow;

Form border properties can affect more than a form’s resizing behavior and edge rendering. If a form has its FormBorderStyle property set to FormBorderStyle.None, no caption is displayed, because no title bar is provided. If the border style is set to one of the tool window styles, the height of the title bar area is reduced, and the system menu isn’t displayed.

By default, forms created with the Visual C# Forms Designer have their FormBorderStyle property set to FormBorderStyle.Sizable. This style usually isn’t suitable for forms that are used as dialog boxes, which typically have a fixed size.

Defining Other Form Properties

In addition to the form’s border style, the Form class includes a number of properties that can be used to control many aspects of the form’s appearance and behavior. Some of the more commonly used properties exposed by the Form class are listed here:

  • BackColor  Specifies the background color for the form. By default, this value is set to Control, which specifies that the form should use the background color specified in the current Windows color scheme.

  • Size  Specifies the dimensions of the form.

  • StartPosition  Specifies the location where the form will be created.

  • Text  Specifies the form’s caption. By default, this value is set to the name of the form; however, the default is usually changed to a more meaningful caption for the form.

  • WindowState  Specifies the current state of the form, which is taken from the WindowState enumeration.

The Color structure is used to represent 32-bit color values in the .NET Framework. The color is specified as a 32-bit value made up of alpha, red, green, and blue (ARGB) components. (The alpha component specifies transparency.) The Color type includes dozens of predefined colors, including common color values such as Red, Green, and White as well as more exotic values such as AliceBlue, BurlyBrown, and SeaGreen. The following code demonstrates using the Color type to change a form’s background color:

BackColor = Color.AliceBlue;

The Color type is discussed in detail in Chapter 14.

The StartPosition property specifies the position of a newly created form. By default, StartPosition is set to WindowsDefaultLocation. This property must be set to one of the values from the FormStartPosition enumeration, listed in Table 11-5.

The FormWindowState property is updated as your application executes. FormWindowState is set to one of the following values, depending on the current state of the form:

  • Minimized  Form is currently minimized.

  • Maximized  Form is currently maximized.

  • Normal  Form is currently at its normal size.

The following code sets several of the form’s properties, including BackColor, Text, and StartPosition:

public Form1()
    FormBorderStyle = FormBorderStyle.FixedToolWindow;
    BackColor = Color.AliceBlue;
    Text = "Basic Form Demo";
    StartPosition = FormStartPosition.CenterScreen;

Part III: Programming Windows Forms