VCL Controls: The Combo Box |
|
Introduction |
A combo box allows the user to select one item from a group. A combo box saves space by using just as much room as an edit control. Like a list box, a combo box displays a list of items to the user. Unlike a list box, there is a version of a combo box that retracts once the user has made a selection; this is useful when space saving is particularly important. Combo Box Properties To add a combo box to your application, from the Standard tab of the Component Palette, click the ComboBox button and click on the form. You can then resize or reposition the control to the desired location. |
Just like every control of your application, the name is the most important property of the control, for you and the compiler. This name allows you and the compiler to refer to the control. By default, the first combo box you add to your form at design time is called ComBox1, the second would be ComboBox2, etc. To change the name of the control, click the Name field, type a new name and press Enter (or click somewhere else). Probably the first this the user sees on a combo box is the text it displays. Although the text of an item is an AnsiString, the items are composed from the TStrings class. To create this list, when the control is selected on the form, on the Object Inspector, click the Items field to reveal an ellipsis button. Click the ellipsis button to display the String List Editor dialog. Type each string and press Enter:
By default, the items you add to the combo box will appear in the order they are supplied. For example the TStrings::Add() method would add the new string at the end of the list. If you want the list of items to be sorted, you can change the value of the Sorted property in the Object Inspector from false (the default) to true. To sort a list programmatically, you can write:
You can unsort the list by changing the value of the Sorted property. If the combo box has a style other than csSimple, there is typically a fixed number of items that display when the user click the control’s arrow. You can control the number of items that displays using the DropDownCount property. By default, this is set to 8. If the list contains a number of items less than the DropdownCount integer value, all of the items would display fine. If the list contains more than the DropDownCount number of items, when the user clicks the arrow, a scroll box would appear. The control would display DropDownCount number of items; to reveal more, the user would have to scroll in the list.
The text that displays on a non-drawn combo box is an AnsiString object. If you want the combo box to display a certain string, use the Text property. At design time, you can set this only if the control’s Style is
csDropDown or csSimple. At run time, you can set the text that would display in the combo box at startup using the
ItemIndex property. This property represents the ordinal integer item of the combo box. The items are counted from 0, then 1, and
so on. The ItemIndex of each item is set depending on the value of the Sorted property. If the list is not sorted, the first item entered in the list has an
ItemIndex value of 0. If the list gets sorted, the first item in ascending order has an
ItemIndex property set at 0. You can therefore use this property to control what item to display in the list:
You can also use the ItemIndex property to find out what item is selected at a given time. Combo Box Methods The TComboBox as a class has only its constructor and its destructor as methods; its other methods are derived from the the parent TCustomComboBox class. The TComboBox constructor is used to dynamically create an instance of the object. If you cannot add the control at design time, declare an instance of the TComboBox, use the new operator to assign the control’s owner for cleaning purposes:
If you want the object to be accessible from more than one function or event, declare an instance of a TComboBox control in the public or private sections of the uses section of the unit or form that would use it:
In the source file, use the new operator to specify the control owner of the object. If you want the control created when the form starts, you can do this in the form’s constructor:
After creating the object, you can manipulate its properties the change the defaults. If you create a local list, you will manipulate it in the same function or event:
If the object were created globally, you can access it anywhere to manipulate it:
|
|
|
||
Home | Copyright © 2004-2007 FunctionX, Inc. | |
|