紧急!怎样删除ListBox中被选中的项?在线等待!(20分)

E

eryu

Unregistered / Unconfirmed
GUEST, unregistred user!
在ListBox1中有许多项,我想设一个按钮Button1,当选中一项后再按Button1,就把该项删除,
请问该怎么做?谢谢!
分不多了,我可以重新开贴给您加上!
 
看帮助文件就应能解决。
ListBox1.Items.Delete(ListBox1.ItemIndex);
 
我现在在按钮事件里这么写的,但删除时程序出错,请问是为什么?
var
i: Integer;
begin
for i:=0 to ListBox1.Items.Count-1do
begin
if ListBox1.Selected then
ListBox1.Items.Delete(ListBox1.ItemIndex);
end;
end;
 
注意,随着你不断删除item,你的ListBox1.Items.Count是不断变化的,而index也在不断
变化,删除不存在的项,所以出错。
 
可以这样做:
var
i: Integer;
begin
for i:=ListBox1.Items.Count-1do
wnto 0do
begin
if ListBox1.Selected then
ListBox1.Items.Delete(i);
end;
end;
 
同意ZW84611的方法,或用一个IF判断
 
接受答案了.
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
872
DelphiTeacher的专栏
D
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
顶部