TWebComboBox: How to set selected item by his value.

I have the next TWebComboBox configuration in dfm.

object ComboBoxCD_COUNTRY: TWebComboBox
Left = 100
Top = 118
Width = 145
Height = 26
ElementClassName = 'form-select'
ElementID = 'ComboBoxCD_COUNTRY'
ElementFont = efCSS
HeightStyle = ssAuto
HeightPercent = 100.000000000000000000
Text = 'ComboBoxCD_COUNTRY'
WidthPercent = 100.000000000000000000
ItemIndex = -1

I assign his values like this:

ComboBoxCD_COUNTRY.Items.Add('United States=USA');

Now I retrieve the current country value from DB, such as 'AUS'.

I want the ComboBox to show the value 'Australia' as the selected one.
But the ItemIndex property in ComboBox has the complete strings assigned; because of it, the next sentence does nothing;

ComboBoxCD_COUNTRY.ItemIndex := ComboBoxCD_COUNTRY.Items.IndexOf('AUS');

To select the value I need to do this:

ComboBoxCD_COUNTRY.ItemIndex := ComboBoxCD_COUNTRY.Items.IndexOf('Australia=AUS');

How can I select the Correct ItemIndex using only the value 'AUS' (example)?

The items are stored in a TStringList and sadly, I cannot see anything in the RTL that provides a direct function to get the index based on just the value. There is a function to get the index based on Name (IndexOfName)

So, a possible approach is:

 for Index := 0 to List.Count - 1 do
      if List.ValueFromIndex[Index] = ValueToFind then
        Break; // Found the value, break the loop

Fwiw, we will add a class helpers for TStrings that will introduce IndexOfValue()

1 Like

The TStrings Helper is a good idea, of course. I'll try it.

The Helpers are a less used resource than its significant contribution deserves.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.