What has happened to ComboBox.Sorted := True; in Delphi 10.2?

故事扮演 提交于 2019-12-20 06:39:55

问题


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


回答1:


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

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