delphiListBox1问题

来源:百度知道 编辑:UC知道 时间:2024/07/04 23:59:07
procedure TForm1.Button2Click(Sender: TObject);
var
Item:Integer;
begin
if ListBox1.SelCount>0 then
begin
Item:=0;
repeat
if ListBox1.selected[Item] then
begin
ListBox1.Items.Delete(Item);
Item:=Item-1;
end;
Item:=Item+1;
until Item=ListBox1.Items.Count; 还有这句 一直不太理解
end;
这里的 Item:=Item-1;
end;
Item:=Item+1; 这两句是什么意思
大哥什么121啊

121

procedure TForm1.Button2Click(Sender: TObject);
var
Item:Integer;
begin
if ListBox1.SelCount > 0 then
begin
Item:=0;
repeat
if ListBox1.selected[Item] then
begin
ListBox1.Items.Delete(Item);
Item:=Item-1;(1)
end;
Item:=Item+1;(2)
until Item=ListBox1.Items.Count;//(3)
end;
end;
这段程序的作用是:点击Button2时,如果ListBox1中有被选中的项,则将选中的项删除之。
Item变量的作用是记录程序当前执行判断ListBox1中是否被选中到第几项了。
(1)处的意思是:程序发现有选中的项,则删除,删除之后那么Item本来标识的那项就没了,所以Item就得-1,
(2)既然程序中有repeat循环,那么肯定要有一个变量的值不断的在改变,注意Item+1是判断列表中的下一项,(3)处意思是判断是否到了列表的最后一项。若是,那就应该跳出repeat循环了,如果没有(2)这句,那么这个程序就是一个死循环。肯定出不了结果。
另外,要实现此程序,ListBox1的MultiSelect属性要设为true,意思是允许多选。