JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Combo Boxes

The Windows forms combo box control is used to display data in a drop-down combo box. The combo box is made up of two parts: The top part is a text box that allows the user to type in all or part of a list item. The other part is a list box that displays a list of items from which the user can select one or more. You can allow the user to select an item from the list, or enter their own data. You can see a combo box from the ComboBoxes example on the CD-ROM in Figure 7.3.


Figure 7.3: A combo box control.

You can set and access the text in a combo box's text box with its Text property. How can you tell which item is selected in a combo box? You can use the SelectedIndex property to get the selected list item. You can change the selected item by changing the SelectedIndex value in code, which will make the corresponding item appear in the text box portion of the combo box. As with list boxes, if no item is selected, the SelectedIndex value is -1. If the first item in the list is selected, then the SelectedIndex value is 0.

In addition, you also can use the SelectedItem property, which is similar to SelectedIndex, but returns the item itself (often a string value). The Items.Count property reflects the number of items in the list (the value of the Items.Count property is always one more than the largest possible SelectedIndex value because SelectedIndex is zero-based).

To add or delete items in a ListBox control, use the Items.Add, Items.Insert, Items.Clear, Items.AddRange, or Items.Remove method. Or you can add and remove items to the list by using the Items property at design time.

By default, a combo box displays a text box with a hidden drop-down list. The DropDownStyle property determines the style of combo box to display. You can set this property to make the combo box's list box a simple drop-down, where the list always displays; a drop-down list box, where the text portion is not editable and you must use an arrow to see the drop-down list box; or the default drop-down list box, where the text portion may be edited and the user must use the arrow key to view the list.

Tip 

To display a list that the user cannot edit, you should use a list box instead of a combo box. In VB6 and before, you used to be able to use the Locked property to make a combo box uneditable, but now you use the Locked property to indicate that a control may not be moved.

You can add and remove items in combo boxes in the same ways as you can with list boxes. You also can use the BeginUpdate and EndUpdate methods to add a large number of items to the ComboBox without the control being redrawn each time an item is added to the list. And the FindString and FindStringExact methods let you to search for an item in the list that contains a particular search string. As with list boxes, you use the SelectedIndex property to get or set the current item, and the SelectedItem property to get or set a reference to the object. And you can also use the Text property to specify the string displayed in the text box part of the combo box.

Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor