Search code examples
comboboxlazarusfreepascal

Get text from specific TComboBox item if it's not selected


How do I get the text from a specific a specific TComboBox item if it's not selected. If I have a combobox with a list of three items and Item1 is the one currently selected:

0: 'ItemText1' (Item1)
1: 'ItemText2' (Item2)
2: 'ItemText3' (Item3)

The text from item 1 is with TComboBox.text is fairly easy, but how do I get the value of item 2 without selecting it? It should be quit easy but I tried various combinations but so far I have no luck (the Lazarus Component Library is currently down so I can't look it up there and all the examples I found used the selected item).

S.th. like "ShowMessage(TComboBox.Items[1].Text)" is what I'm looking for... (the output should be "Item2").


Solution

  • The Items property of a ComboBox is of type TStrings which behaves like a zero-based array of strings which you can read by supplying a numeric index. E.g.

    ShowMessage(ComboBox1.Items[0]);  // shows the first item, ItemText1 in your example
    

    It works regardless of which item, if any, is selected in the gui.

    Simple as that.