Link to home
Start Free TrialLog in
Avatar of firekiller15

asked on

Delphi combo BOx

i want to learn how to use combo Box step by step. Start from begining .anyone can direct me or give me good website on this topic?
Avatar of rfwoolf
Flag of South Africa image

The TCombobox control is available from the Standard section of the Tool Palette
1) Double-click or drag the ComboBox onto your Form - this creates an instance of the combobox on your form. - it's name property (if you check the object inspector) might now be ComboBox1.

The key to the Combobox is its Items property. Select ComboBox1, and in the object inspector, you will see the property "Items" and it will say this property is of type (TStrings). In other words the Items are a collection of strings.
2) In the object inspector under Items, you will see an elipsis button (...), click it and a window should open that allows you to enter in a list of items. Use each line for an item, example:
  This is option 1
  This is option 2
  This is option 3

Note: It is also possible to set these items at runtime (programmatically), by using a combonation of these commands:
Combobox1.Items.Clear; //clears all the items
Combobox1.Items.Add('Something'); //Adds an item called 'Something'
Combobox1.Items.Insert(0, 'Something'); //Inserts into your Items list, in position 0 (which is the first item) the entry 'Something'

3) Finally, take note of the ItemsIndex property of the Combobox. This option sets which particular item in the Items list gets shown to the user. -1 means no item is shown, 0 means the first item, 1 means the second item etc.
In this case we will set it to 0, and the first item in your list should display

Note: Again, we can do this programmatically, by saying
Combobox1.ItemIndex := 0;

I hope this helps
That's basically how it all works, and if you're using a TDBComboBox (a data-aware Combo Box) then it's very similar except the Items list is populated by your database/table. You would set its Datasource property and the DataField property for the field whose values you want to use for your Items.

Getting back to the normal TComboBox, there are 1 or 2 more tricks I haven't mentioned yet.

Here's how to go through each item in the combobox:
  i : integer;
 for i  := 0 to Combobox1.Items.count - 1 do
  showmessage(Combobox1.Items[i])); //in this example we are using showmessage
Avatar of firekiller15


if i want only want to display the result of the item that i select , how?
let say i have 3 item in my combobox
if A print how are you
if B print are how you
if C print you are how
Avatar of rfwoolf
Flag of South Africa image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial