【问题标题】:How to find an Item's index in a Firemonkey TListBox?如何在 Firemonkey TListBox 中找到项目的索引?
【发布时间】:2018-08-15 23:22:18
【问题描述】:

我的目标是更新TListBoxItemIndex,这样当以编程方式将项目添加到列表时,相应的行会在TListBox 上标出。

我试过了:

with MyLstBox do begin
  ItemIndex := -1;
  for ind := 0 to Pred (Items.Count) do
    if InsertedString = Items [ind]) then begin
      ItemIndex := Ind;
      Break;
    end;
end;

这段代码勾勒了刚刚插入的项目,但也保持了前一个插入项目的轮廓。

MultiSelect 设置为False,所以原则上只需要概述一项。

【问题讨论】:

    标签: delphi listbox firemonkey delphi-10.2-tokyo


    【解决方案1】:

    Items 属性是一个TStrings 对象。您可以使用TStrings.IndexOf() 方法代替手动循环:

    MyLstBox.ItemIndex := MyLstBox.Items.IndexOf(InsertedString);
    

    向 ListBox 添加新项时,TStrings.Add() 方法返回新项的索引:

    MyLstBox.ItemIndex := MyLstBox.Items.Add(InsertedString);
    

    【讨论】:

    • 好的。但是还有另一个问题:为什么预览插入的项目仍然突出显示?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-10
    • 2017-10-09
    • 2011-10-05
    相关资源
    最近更新 更多