请教下面的语句怎样删除LISTBOX1的项目(0分)

  • 主题发起人 topdelphi
  • 开始时间
T

topdelphi

Unregistered / Unconfirmed
GUEST, unregistred user!
请教下面的语句怎样删除LISTBOX1的项目,我的功能是单选LISTBOX1到LISTBOX2之后,
就删除LISTBOX1的内容,但是下面的删除语句出错了啊,请指教如何修改.谢谢

procedure TForm1.Button1Click(Sender: TObject);
var //这个是单选择功能,但是不能删除LISTBOX1记录
i:integer;
begin
for i:=0 to listbox1.Items.Count -1 do
begin
if listbox1.Selected then
begin
listbox2.Items.Add(listbox1.Items.Strings);
listbox1.Items.clear ;
end;
end;
end;
 
方法这样更好:
procedure TForm1.Button1.Click(Sender: TObject);
begin
ListBox2.Items.Add(ListBox1.Items[ListBox1.ItemIndex]);
ListBox1.Clear;
end;
你的报错的原因是当清空后再判断Selected就会出错,它已经不存在了。
 
为和不用
Try
...
except
..
end;
来完成!
 
试试这个:
procedure TForm1.Button1Click(Sender: TObject);
begin
for i := 0 to ListBox1.Items.Count - 1 do
begin
if ListBox1.ItemIndex = i then
begin
ListBox2.Items.Add( ListBox1.Items );
ListBox1.DeleteSelected;
end;
end;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var //这个是单选择功能,但是不能删除LISTBOX1记录
i:integer;
begin
for i:=0 to listbox1.Items.Count -1 do
begin
if listbox1.Selected then
begin
listbox2.Items.Add(listbox1.Items.Strings);
end;
end;
ListBox1.items.delete(Listbox1.itemindex);
end;
 
谢谢,我已经完成了单选择的功能,但我还有个全选的功能按钮,语句如下,请问题还是不能
删除LISTBOX1的全部的ITEM啊,请指教修改
procedure TForm1.Button2Click(Sender: TObject);
begin
for j:=0 to listbox1.Items.Count -1 do
begin
listbox2.Items.Add(listbox1.Items[j] );
listbox1.Clear;
end;
end;
 
先判断listbox1.Items.Count 数量,在运行你的添加代码
 
ListBox1.Clear是不用循环的。
 
顶部