请高手过来看看我的代码!谢谢

lesnme 2002-05-20 09:15:59
该程序是把文路径添加到一个列表listbox 中,第一次,第二添加的时候正确
如果你多添加几次,所添加的项目将是以上添加的平方倍,不知道为何?
请高手指导:你只要添加4 个控件就可以-- directorylistbox,drivecombox,button
listbox;

procedure TautoDelform.ButtonClick(Sender: TObject);
var
i:integer;
filestr:string;
begin
timer1.Enabled:=false;
filestr:=DirectoryLb.Directory;
if listbox1.Items.count=0 then
ListBox1.Items.add(filestr)
else
begin
for i:=0 to (ListBox1.Items.Count-1) do
begin
if(ListBox1.Items[i]=filestr) then
Exit

else
ListBox1.Items.add(filestr);
end;
end;
end; // if
...全文
37 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
linx88 2002-05-20
  • 打赏
  • 举报
回复
procedure TautoDelform.Button1Click(Sender: TObject);
var
i,j:integer;
filestr:string;
b:boolean;
begin
timer1.Enabled:=false;
b:=false;
filestr:=Directorylb.Directory;
if listbox1.Items.count=0 then
ListBox1.Items.add(filestr)
else
begin
j:=ListBox1.Items.Count-1;
i:=0;
while (i<j)and(not b)do
begin
if(ListBox1.Items[i]=filestr) then
begin
b:=true;
end;
i:=i+1;
end;
if not b then
ListBox1.Items.add(filestr);
end;
end;
linx88 2002-05-20
  • 打赏
  • 举报
回复
或者
没有啊?
procedure TautoDelform.Button1Click(Sender: TObject);
var
i,j:integer;
filestr:string;
b:boolean;
begin
timer1.Enabled:=false;
b:=false;
filestr:=Directorylb.Directory;
if listbox1.Items.count=0 then
ListBox1.Items.add(filestr)
else
begin
j:=ListBox1.Items.Count-1;
for i:=0 to j do
begin
if(ListBox1.Items[i]=filestr) then
begin
b:=true;
Exit ;
end;
end;
if not b then
ListBox1.Items.add(filestr);
end;
end;
linx88 2002-05-20
  • 打赏
  • 举报
回复
把你改后的代码再列出来,我这运行没问题的。
lesnme 2002-05-20
  • 打赏
  • 举报
回复
tswrl 说的很有道理,可是我不知道如果才能达到目的--是添加项目不重复
lesnme 2002-05-20
  • 打赏
  • 举报
回复
如果该正?
lesnme 2002-05-20
  • 打赏
  • 举报
回复
如果你持续的选几项就出错呀
tswrl 2002-05-20
  • 打赏
  • 举报
回复
你的程序的本意是先查找ListBox如果到就不添加,找不到就家如,可是只有变量整个ListBox后才能确定是否改添加,可在你的程序中,在做第一次比较是如不相等即便Listbox中存在该项但不再第一位时,也会被再次添加进去,这就是你的程序的错误!
linx88 2002-05-20
  • 打赏
  • 举报
回复
可以,我试过
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
filestr:string;
b:boolean;
begin
// timer1.Enabled:=false;
b:=false;
filestr:=Directorylistbox1.Directory;
if listbox1.Items.count=0 then
ListBox1.Items.add(filestr)
else
begin
for i:=0 to (ListBox1.Items.Count-1) do
begin
if(ListBox1.Items[i]=filestr) then
begin
b:=true;
Exit ;
end;
end;
if not b then
ListBox1.Items.add(filestr);

end;

end;
lesnme 2002-05-20
  • 打赏
  • 举报
回复
您还能帮我再想想吗?我想了好久
lesnme 2002-05-20
  • 打赏
  • 举报
回复
楼上的兄弟,如果这样是添加不了什么东西
linx88 2002-05-20
  • 打赏
  • 举报
回复
如果不一样,你相当执行:
for i:=0 to (ListBox1.Items.Count-1) do
begin
ListBox1.Items.add(filestr);
end;
DeathCat 2002-05-20
  • 打赏
  • 举报
回复
在每次添加之前调用。
listbox.items.clear;

具体还得看你的代码。如果不能达到目的,就把代码贴出来看看。
linx88 2002-05-20
  • 打赏
  • 举报
回复
var
bol:boolean;
bol:=false;
for i:=0 to (ListBox1.Items.Count-1) do
begin
if(ListBox1.Items[i]=filestr) then
begin
bol:=true;
Exit;
end;
end;
if not bol then
ListBox1.Items.add(filestr);
black_fox 2002-05-20
  • 打赏
  • 举报
回复
你好象没有清空哦。。hehe

将self.ListBox1.Items.Clear加上去看看。。

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧