Visual Basic 2005 for Programmers (2nd Edition)
14.8. ComboBox Control
The ComboBox control combines TextBox features with a drop-down lista GUI component that contains a list from which a value can be selected. A ComboBox usually appears as a TextBox with a down arrow to its right. By default, the user can enter text into the TextBox or click the down arrow to display a list of predefined items. If a user chooses an element from this list, that element is displayed in the TextBox. If the list contains more elements than can be displayed in the drop-down list, a scrollbar appears. The maximum number of items that a drop-down list can display at one time is set by property MaxDropDownItems. Figure 14.21 shows a sample ComboBox in three different states. Figure 14.21. ComboBox demonstration.
As with the ListBox control, you can add objects to collection Items programmatically, using methods Add and AddRange, or visually, with the String Collection Editor. Figure 14.22 lists common properties and a common event of class ComboBox.
Look-and-Feel Observation 14.4
Property DropDownStyle determines the type of ComboBox and is represented as a value of the ComboBoxStyle enumeration, which contains values Simple, DropDown and DropDownList. Option Simple does not display a drop-down arrow. Instead, a scrollbar appears next to the control, allowing the user to select a choice from the list. The user also can type in a selection. Style DropDown (the default) displays a drop-down list when the down arrow is clicked (or the down-arrow key is pressed). The user can type a new item in the ComboBox. The last style is DropDownList, which displays a drop-down list but does not allow the user to type in the ComboBox. The ComboBox control has properties Items (a collection), SelectedItem and SelectedIndex, which are similar to the corresponding properties in ListBox. There can be at most one selected item in a ComboBox. If no items are selected, then SelectedIndex is -1. When the selected item changes, a SelectedIndexChanged event occurs. Class FrmComboBoxTest (Fig. 14.23) allows users to select a shape to drawcircle, ellipse, square or pie (in both filled and unfilled versions)by using a ComboBox. The ComboBox in this example is uneditable, so the user cannot type in the ComboBox. Figure 14.23. ComboBox used to draw a selected shape.
Look-and-Feel Observation 14.5
After creating ComboBox cboImage, make it uneditable by setting its DropDownStyle to DropDownList in the Properties window. Next, add items Circle, Square, Ellipse, Pie, Filled Circle, Filled Square, Filled Ellipse and Filled Pie to the Items collection using the String Collection Editor. Whenever the user selects an item from cboImage, a SelectedIndexChanged event occurs and event handler cboImage_SelectedIndexChanged (lines 541) executes. Lines 915 create a Graphics object, a Pen and a SolidBrush, which are used to draw on the Form. The Graphics object (line 9) allows a pen or brush to draw on a component using one of several Graphics methods. The Pen object (line 12) is used by methods DrawEllipse, DrawRectangle and DrawPie (lines 23, 25, 27 and 29) to draw the outlines of their corresponding shapes. The SolidBrush object (line 15) is used by methods FillEllipse, FillRectangle and FillPie (lines 31, 33, 35 and 37) to fill their corresponding solid shapes. Line 18 colors the entire Form White, using Graphics method Clear. These methods are discussed in greater detail in Chapter 17. The application draws a shape based on the selected item's index. The Select Case statement (lines 2138) uses cboImage.SelectedIndex to determine which item the user selected. Graphics method DrawEllipse (line 23) takes a Pen, the x- and y-coordinates of the center and the width and height of the ellipse to draw. The origin of the coordinate system is in the upper-left corner of the Form; the x-coordinate increases to the right, and the y-coordinate increases toward the bottom of the Form. A circle is a special case of an ellipse with equal width and height. Line 23 draws a circle. Line 27 draws an ellipse that has different values for width and height. Class Graphics method DrawRectangle (line 25) takes a Pen, the x- and y-coordinates of the upper-left corner and the width and height of the rectangle to draw. Method DrawPie (line 29) draws a pie as a portion of an ellipse. The ellipse is bounded by a rectangle. Method DrawPie takes a Pen, the x- and y-coordinates of the upper-left corner of the rectangle, its width and height, the start angle (in degrees) and the sweep angle (in degrees) of the pie. Angles increase clockwise. The FillEllipse (lines 31 and 35), FillRectangle (line 33) and FillPie (line 37) methods are similar to their unfilled counterparts, except that they take a SolidBrush instead of a Pen. Some of the drawn shapes are illustrated in the screen shots of Fig. 14.23. |