开发者

How can a list box disallow duplicate items?

开发者 https://www.devze.com 2022-12-17 13:12 出处:网络
I was looking at http:开发者_如何学Go//delphi.about.com/od/tlistbox/a/list-box-onchange-drag-drop.htm

I was looking at http:开发者_如何学Go//delphi.about.com/od/tlistbox/a/list-box-onchange-drag-drop.htm and I was wondering if it would be possible to add the ability to disallow duplicate items like this, and if so how would I go and do it?

Thanks

-Brad


To prevent duplicates in a list box, simply check whether the intended item exists in the list before you add it.

function ItemExists(ListBox: TListBox; const Item: string): Boolean;
begin
  Result := ListBox.Items.IndexOf(Item) >= 0;
end;

Call that function before you call Items.Add. If it returns True, don't call Items.Add.


I often use;

var
  item1 : string;
begin
  item1 := Trim(eSym1.Text);
  if ListBox1.Items.IndexOf(item1) < 0 then
    ListBox1.Items.Add(item1);
end; 
0

精彩评论

暂无评论...
验证码 换一张
取 消