Having recently received a 'Tumbleweed' badge for my last question, I am not sure whether I should be asking any more questions, but here goes.
I am populating a TComboBox
with items from a sqlite table and this works fine. In my previous version of Delphi I was able to use ComboBox1.Sorted := True;
to sort the items, but this seems to have disappeared in Delphi 10.2. I can sort the items in the table by applying a query and then populate the TComboBox
from the sorted table. However, for curiosities sake I would like to find out how one now sorts items in a TComboBox
. I have found some references to TComboBox(Sort:Compare)
but have not succeeded in getting this to work to as of yet.
Can somebody please shed some light on this - many thanks
In Firemonkey you can populate a TComboBox
instance either simply with the Items
property of type TStrings
or you add TListBoxItem
instances with the form designer. But internally always TListBoxItem
for the elements is used.
To use the TComboBox.Sort
you need to provide an anonymous compare-function.
This is a simple example usage of TComboBox.Sort
cbxItems.Sort(
function (pLeft, pRight: TFMXObject): Integer
var
lLeft, lRight: TListBoxItem;
begin
lLeft := TListBoxItem(pLeft);
lRight := TListBoxItem(pRight);
Result := String.Compare(lLeft.Text, lRight.Text);
end
);
来源:https://stackoverflow.com/questions/52606098/what-has-happened-to-combobox-sorted-true-in-delphi-10-2