
TComboBox
Hierarchy: TObject - TPersistent - TControl - TWinControl - TCustomComboBox - TComboBox
This control combines a scrolling list with a text box. The text area is used to locate the item in the list, to save scrolling to find it, though the user has the choice of typing or scrolling.
The ComboBox is more likely to be used for input than output, allowing a user to select an option from a drop-down list. The ListBox is a better choice for output of data.
Rows within a combo box behave like lines in a list box, that is the item selected can be processed by the act of clicking on it and selecting it.
There are three options in the Style property of a ComboBox:
This simple example uses the Change event of the ComboBox control to change the colour of the form:
procedure TForm1.ComboBox1Change(Sender: TObject);
var
i: integer;
color:string;
begin
i:=combobox1.ItemIndex;
case i of
0: form1.Color:=clred;
1: form1.Color:=clgreen;
2:form1.Color:=clblue;
end;
end;
The case line could read:
case combobox1.ItemIndex of
in which case there is no need for the i variable.
The Change event is the default event for the ComboBox, which makes this control ideal for taking action on the choice of an item in a list. Note that an item is chosen not by its contents but by its position in the list of items. These are indexed from 0