永发信息网

delphi怎么判断然后添加?

答案:4  悬赏:20  手机版
解决时间 2021-07-26 00:56
  • 提问者网友:趣果有间
  • 2021-07-25 00:34

在edit中输入一个数

怎么判断它与listbox的中的数据数否有重复的

没有重复的就添加在listbox,重复的就不要添加?

最佳答案
  • 五星知识达人网友:怀裏藏嬌
  • 2021-07-25 01:09

需要那么复杂吗? 就一行代码。


procedure TForm1.Button1Click(Sender: TObject);
begin
with ListBox1.Items do if IndexOf(edit1.Text)<0 then Add(Edit1.Text);
end;

全部回答
  • 1楼网友:低血压的长颈鹿
  • 2021-07-25 03:56

天哪。。使用listbox.item.indexof(edit1.text)如果返回-1则说明不存在,

如果存在,则返回所在的index。。。又必要弄那么复杂么。。 巨汗。。

  • 2楼网友:煞尾
  • 2021-07-25 03:32

在edit1.OnKeyPress事件中添加代码

procedure TForm1.edt1KeyPress(Sender: TObject; var Key: Char);

var

i: integer;

find: boolean; begin

find:= false;

for i:= 0 to ListBox1.Items.Count - 1 do

begin

if ListBox1.Items[i] = Edit1.Text then

begin

find:= true;

break;

end;

end;

if not find then ListBox1.Items.Add(Edit1.Text);

end;

  • 3楼网友:神的生死簿
  • 2021-07-25 02:06
procedure TForm1.Button1Click(Sender: TObject); var count: Integer; TheSame: Boolean; begin TheSame := False; //假设不一样 for count := 0 to ListBox1.Count - 1 do if Edit1.Text = ListBox1.Items[count] then TheSame := not TheSame; if not TheSame then //如果不一样就添加 ListBox1.Items.Add(Edit1.Text); end;
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯