Windows Forms

Windows Forms are used to create the GUIs for programs. A Form is a graphical element that appears on your computer's desktop; it can be a dialog, a window or an MDI window (multiple document interface window)discussed in Chapter 14, Graphical User Interface Concepts: Part 2. A component is an instance of a class that implements the IComponent interface, which defines the behaviors that components must implement, such as how the component is loaded. A control, such as a Button or Label, has a graphical representation at runtime. Some components lack graphical representations (e.g., class Timer of namespace System.Windows.Formssee Chapter 14). Such, components are not visible at run time.

Figure 13.3 displays the Windows Forms controls and components from the C# Toolbox. The controls and components are organized into categories by functionality. Selecting the category All Windows Forms at the top of the Toolbox allows you to view all the controls and components from the other tabs in one list (as shown in Fig. 13.3). In this chapter and the next, we discuss many of these controls and components. To add a control or component to a Form, select that control component or from the Toolbox and drag it on the Form. To deselect a control or component, select the Pointer item in the Toolbox (the icon at the top of the list). When the Pointer item is selected, you cannot accidentally add a new control to the Form.

Figure 13.3. Components and controls for Windows Forms.

(This item is displayed on page 600 in the print version)

When there are several windows on the screen, the active window is the frontmost and has a highlighted title bartypically darker blue than the other windows on the screen. A window becomes the active window when the user clicks somewhere inside it. The active window is said to "have the focus." For example, in Visual Studio the active window is the Toolbox when you are selecting an item from it, or the Properties window when you are editing a control's properties.

A Form is a container for controls and components. When you drag a control or component from the Toolbox on the Form, Visual Studio generates code that instantiates the object and sets its basic properties. This code is updated when the control or component's properties are modified in the IDE. If a control or component is removed from the Form, the generated code for that control is deleted. The generated code is placed by the IDE in a separate file using partial classes. Although we could write this code ourselves, it is much easier to create and modify controls and components using the Toolbox and Properties windows and allow Visual Studio to handle the details. We introduced visual programming concepts in Chapter 2. In this chapter and the next, we use visual programming to build more substantial GUIs.

Each control or component we present in this chapter is located in namespace System.Windows.Forms. To create a Windows application, you generally create a Windows Form, set its properties, add controls to the Form, set their properties and implement event handlers (methods) that respond to events generated by the controls. Figure 13.4 lists common Form properties, methods and events.

Figure 13.4. Common Form properties, methods and events.

Form properties, methods and events

Description

Common Properties

AcceptButton

Button that is clicked when Enter is pressed.

AutoScroll

Boolean value that allows or disallows scrollbars when needed.

CancelButton

Button that is clicked when the Escape key is pressed.

FormBorderStyle

Border style for the Form (e.g., none, single, three-dimensional).

Font

Font of text displayed on the Form, and the default font for controls added to the Form.

Text

Text in the Form's title bar.

Common Methods

Close

Closes a Form and releases all resources, such as the memory used for the Form's controls and components. A closed Form cannot be reopened.

Hide

Hides a Form, but does not destroy the Form or release its resources.

Show

Displays a hidden Form.

Common Event

Load

Occurs before a Form is displayed to the user. The handler for this event is displayed in the Visual Studio editor when you double click the Form in the Visual Studio designer.

When we create controls and event handlers, Visual Studio generates much of the GUI-related code. In visual programming, the IDE maintains GUI-related code and you write the bodies of the event handlers to indicate what actions the program should take when particular events occur.

Event Handling

Категории