Change TCombobox Index AND its Text in Delphi FMX

Viewed 422

I am trying to change the Index/ItemIndex of a combobox to make the combobox change its text to the index which belongs to the title of a marker on a map.. Initially this combobox is used to assign some text to a marker which works fine, but when I try to use this combobox to "edit" the title of the marker by assigning the combobox the correct index of a given text which definetely is an item of the combobox the combobox doesnt change its text shown.. for debugging purposes I added the showmessage dialogs and they show me the correct one perfectly fine, but the text of the combobox still doesnt change... what am I doing wrong?

ComboBox1.Index := -1;
for I := 0 to ComboBox1.Items.Count - 1 do
begin
    if ComboBox1.Items[I].Equals(Marker.Descriptor.Title)
    then
    begin
        showmessage(ComboBox1.Items[I]+' is working!!!');
        ComboBox1.ItemIndex := I;
        ComboBox1.Index := I;
    end else begin
      showmessage(ComboBox1.Items[I]+' is not working');
    end;
end;

using ComboBox1.ItemIndex := I; or ComboBox1.Index := I; should change the text of the combobox, right?

1 Answers

Your problem is the line ComboBox1.Index := -1; which makes the combobox disappear. Index and ItemIndex have totally different purpose. Look at the documentation for index and ItemIndex.

This code work for me (I've put an Edit instead of Marker.Descriptor.Title for easy testing):

procedure TForm1.Button1Click(Sender: TObject);
var
    I : Integer;
begin
    for I := 0 to ComboBox1.Items.Count - 1 do begin
        if ComboBox1.Items[I].Equals(Edit1.Text) then begin
            ComboBox1.ItemIndex := I;
            break;
        end;
    end;
end;
Related