簡體   English   中英

如何在 Firemonkey TListBox 中查找項目的索引?

[英]How to find an Item's index in a Firemonkey TListBox?

我的目標是更新ItemIndex一個的TListBox ,以一種編程方式將項目添加到列表時,相應的線路上列出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 ,因此原則上只應勾勒出一項。

Items屬性是一個TStrings對象。 您可以使用TStrings.IndexOf()方法而不是手動循環:

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

當您向 ListBox 添加新項目時, TStrings.Add()方法返回新項目的索引:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM