14.2 Windows Forms

Windows Forms is one library that can be used to create GUIs—you’ll also learn about Universal Windows Platform (UWP) and Windows Presentation Foundation (WPF) in online chapters. 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 15. A component is an instance of a class that implements the IComponent interface, which defines the behaviors that components must implement. A control, such as a Button or Label, is a component that has a graphical representation at runtime. Some components lack graphical representations (e.g., class Timer of namespace System.Windows.Forms—see Chapter 15). Such components are not visible at run time.

Figure 14.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. 14.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).

Fig. 14.3 Components and controls for Windows Forms.

Active Window and Focus

When there are several windows on the screen, the active window is the frontmost and has a highlighted title bar. 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’re selecting an item from it, or the Properties window when you’re editing a control’s properties.

Auto-Generated Code Stored in Separate File

A Form is a container for controls and components. When you drag an item from the Tool-box onto the Form, Visual Studio generates code that creates the object and sets its basic properties. This code is updated when the control or component’s properties are modified in the IDE. Removing a control or component from the Form deletes the corresponding generated code. The IDE maintains the generated code in a separate file using partial classes—classes that are split among multiple files and assembled into a single class by the compiler. You could write this code yourself, but it’s much easier to allow Visual Studio to handle the details. We introduced visual programming concepts in Section 2.6. In this chapter and the next, we use visual programming to build more substantial GUIs.

Common Form Properties, Methods and an Event

Each control or component we present in this chapter is located in namespace System.Windows.Forms. To create a Windows Forms app, 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 when a user interacts with the controls. Figure 14.4 lists common Form properties, common methods and a common event.

Fig. 14.4 Common Form properties, methods and an event.

Form properties, methods and an event Description
Common Properties
AcceptButton Default Button that’s clicked when you press Enter.
AutoScroll bool value (false by default) that allows or disallows scrollbars when needed.
CancelButton Button that’s clicked when the Escape key is pressed.
FormBorderStyle Border style for the Form (Sizable by default).
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 contents. 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. You’ll learn about events and event-handling in the next section.
..................Content has been hidden....................

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