Search code examples
delphidelphi-xe7delphi-xe8

how to sort items inside a combobox?


I have a combobox that I add some numbers to, like in the following code

combobox1.Items.BeginUpdate;
try
  combobox1.Sorted := True;
  combobox1.Items.Add('0');
  combobox1.Items.Add('2');
  combobox1.Items.Add('1');
  combobox1.Items.Add('3');
  combobox1.Items.Add('5');
  combobox1.Items.Add('4');
finally
  combobox1.Items.EndUpdate;
end;

I want to sort these numbers as 0,1,2,3,4,5... and so on, inside the combobox.

I enabled the Sorted property but items did not sort.

How could I possibly sort items with numbers inside the combobox ?

I load items in combobox from TList with this code:

var
  J : integer;
  themes : Tthemes;
begin
  ComboBox1.Items.BeginUpdate;
  try
    ComboBox1.Sorted := True;
    for J := 0 to listitems.Count - 1 do
    begin
      themes := listitems.Items[J];
      ComboBox1.Items.Add(themes.designid);
    end;
  finally
    ComboBox1.Items.EndUpdate;
  end;

  ComboBox1.ItemIndex := 0;

Solution

  • Try this:

      combobox1.Sorted := False;
      combobox1.Items.Add('0');
      combobox1.Items.Add('2');
      combobox1.Items.Add('1');
      combobox1.Items.Add('3');
      combobox1.Items.Add('5');
      combobox1.Items.Add('4');
      combobox1.Sorted := True;