The Button Class The CheckBox Class The ComboBox Class

187

Chapter 5. Windows Forms II: Controls, Common Dialog Boxes, and Menus

By themselves, one or more forms provide very little functionality to most desktop applications. For the most part, forms are valuable insofar as they serve as containers for controls. In this chapter, well complete our discussion of building desktop applications by focusing on the objects that forms contain—in particular, controls and components, common dialogs, and menus.

5.1 Common Controls and Components

This section contains a summary of the controls and components defined in the System.Windows.Forms namespace. Components are classes derived from the Component class defined in the System.ComponentModel namespace. They may or may not provide a visual interface. They are often used as elements of forms but dont have to be. Controls are classes derived from the Control class defined in the System.Windows.Forms namespace. Controls generally are used to build the visual appearance of a form. The Control class itself is derived from the Component class, so controls are also components. The common dialog boxes are not listed here, even though they all derive from the Component class. They are given their own section, Sect ion 5.4 later in this chapter.

5.1.1 The Button Class

This class represents a button control, which is one of the most commonly used controls in Windows applications. The Button classs Click event, which it inherits from Control, is its most commonly used event. The Button class inherits two important properties from ButtonBase: FlatStyle and Image. The first determines the appearance of the button and can take any value of the FlatStyle enumeration: Flat , Popup , Standard the default, and System . Buttons with these four settings are shown in Figur e 5- 1 . Assigning FlatStyle.System as the value of the FlatStyle property makes the appearance of the button dependent on the operating system. Figure 5-1. Various appearances of the Button control The Image property allows you to embed an image into a button. The following code shows how to programmatically set the Image property of Button: Button1.Image = New System.Drawing.Bitmapfilepath

5.1.2 The CheckBox Class

The CheckBox class represents a checkbox control. Its appearance is determined by its Appearance property, which can take either value of the Appearance enumeration: Button or Normal the default. The Button value is rarely used because this setting makes the checkbox look like a Button control. 188 The CheckBox classs Checked property can be set to True to make the checkbox checked or False to uncheck it.

5.1.3 The ComboBox Class

Both the ComboBox and ListBox classes derive from the ListControl class; therefore, the ComboBox class is very similar to the ListBox class and has properties and methods similar to those of the ListBox class. Refer to Sect ion 5.1.9 for information on these common properties and methods. The following properties are specific to the ComboBox class: DropDownStyle Defines the drop-down style of the ComboBox. It can take any value of the ComboBoxStyle enumeration: DropDown the default value, DropDownList , and Simple . Both DropDown and DropDownList require the user to click the arrow button to display the drop-down portion of the ComboBox; however, DropDown allows the user to edit the text portion of the ComboBox. Simple makes a ComboBoxs text portion editable and its drop-down portion always visible. DroppedDown Specifies whether the drop-down portion of a ComboBox is visible.

5.1.4 The DateTimePicker Class