How to sort the items of a TcxImageComboBox by Description?

南楼画角 提交于 2019-12-08 06:24:25

问题


I've found a good component to implement a Caption<->Value List for ComboBox:

Is there a ComboBox that has Items like a TcxRadioGroup?

The only problem is: It has a Sorted property, but that doesn't work.

So, how do I sort the Items of a TcxImageComboBox?


回答1:


Quick and dirty method, should work fine for most cases:

function CompareItems(AFirst: TcxImageComboBoxItem; ASecond: TcxImageComboBoxItem): Integer;
begin
  Result := AnsiCompareText(AFirst.Description, ASecond.Description);
end;

procedure SortCxComboBoxItems(AItems: TcxImageComboBoxItems);
var
  I    : Integer;
  J    : Integer;
  PMin : Integer;
begin
  AItems.BeginUpdate;
  try
    // Selection Sort (http://en.wikipedia.org/wiki/Selection_sort)
    for I := 0 to AItems.Count - 1 do 
    begin
      PMin := I;
      for J := I + 1 to AItems.Count - 1 do 
      begin
        if CompareItems(AItems[J], AItems[PMin]) < 0 then begin
          PMin := J;
        end;
      end;
      if PMin <> I then 
      begin
        AItems[PMin].Index := I;
      end;
    end;
  finally
    AItems.EndUpdate;
  end;
end;


来源:https://stackoverflow.com/questions/8185796/how-to-sort-the-items-of-a-tcximagecombobox-by-description

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!