问题
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