Visual Basic 2005 for Programmers (2nd Edition)
13.2. 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. 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 execution time. Figure 13.3 displays the Windows Forms controls and components from the Visual Basic 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 or component from the Toolbox and drag it onto the Form. To deselect a control or component, select the Pointer item in the Toolbox (the icon at the top of the list). Figure 13.3. Components and controls for Windows Forms.
When there are several windows on the screen, the active window is the frontmost and has a highlighted title bartypically darker 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 onto the Form, Visual Studio generates code that instantiates the object and sets its basic properties to default values. 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 (introduced in Section 9.6). 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.
When you 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. |
Категории