【问题标题】:how to sort items inside a combobox?如何对组合框中的项目进行排序?
【发布时间】:2016-07-24 21:10:10
【问题描述】:

我有一个组合框,可以添加一些数字,如下面的代码所示

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;

我想在combobox 中将这些数字排序为 0、1、2、3、4、5...等等。

我启用了 Sorted 属性,但项目没有排序。

我怎么可能对 combobox 中带有数字的项目进行排序?

我使用以下代码从 TList 加载 combobox 中的项目:

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;

【问题讨论】:

  • 我试过你的代码,结果是排序的! XE7 并假设为 Vcl 项目。
  • 奇怪的是我做了相同的代码,结果与他们添加的相同,但没有在 dll 项目中使用此代码对 iam 进行排序
  • 我无法想象作为 dll 的项目会产生什么影响。但以防万一,请尝试使用新的 vcl 表单项目。将组合框和一个按钮拖放到表单上。在按钮 OnClick 事件中复制 - 粘贴上面的代码。没有其他设计时设置。
  • 我在单个 exe 文件中对其进行了正常排序测试。也许是因为我加载的记录来自 tlist ?这会有所作为吗?添加了更多代码
  • ComboBox 项目仅按字母顺序排序,而不是按数字排序。如果您需要后者,请将项目复制到单独的 TStringList,使用其 CustomSort() 方法对其进行排序(在排序期间将字符串转换为整数),然后将其复制回 ComboBox。

标签: delphi delphi-xe7 delphi-xe8


【解决方案1】:

试试这个:

  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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多