我想用鼠标在listbox中选中一条记录,然后按键删除对应的记录
程序一
procedure TForm2.Button2Click(Sender: TObject);
var i:integer;
begin
for i:=0 to (ListBox1.Items.Count-1) do
if listbox1.Selected[i] then
listbox1.Items.Delete(i);
end;执行程序出错“list index out of bounds(14)”程序二
procedure TForm2.Button2Click(Sender: TObject);
var i:integer;
begin
for i:=(ListBox1.Items.Count-1) downto 0 do
if listbox1.Selected[i] then
listbox1.Items.Delete(i);
end;
执行程序正确请都哪位高人能给指点一下,为什么会这样啊?
程序一
procedure TForm2.Button2Click(Sender: TObject);
var i:integer;
begin
for i:=0 to (ListBox1.Items.Count-1) do
if listbox1.Selected[i] then
listbox1.Items.Delete(i);
end;执行程序出错“list index out of bounds(14)”程序二
procedure TForm2.Button2Click(Sender: TObject);
var i:integer;
begin
for i:=(ListBox1.Items.Count-1) downto 0 do
if listbox1.Selected[i] then
listbox1.Items.Delete(i);
end;
执行程序正确请都哪位高人能给指点一下,为什么会这样啊?
这是我写的,你看看
var
I: Integer;
begin
if LBSource.SelCount = 0 then exit;
I := 0;
while I <= LBSource.Items.Count - 1 do
begin
if LBSource.Selected[I] then
begin
LBSource.Items.Delete(I);
I := I - 1;
end;
I := I + 1;
end;
begin
if ListBox1.ItemIndex<0 then//未选中记录
showmessage('请选择记录')
else//选中记录
ListBox1.Items.Delete(ListBox1.ItemIndex);
end;
以上语句比你的好。
if listbox1.ItemHeight>0 then
listbox1.DeleteSelected;
var
i: integer;
begin
i := 0;
while i < ListBox1.Count do begin
if ListBox1.Selected[i] then
ListBox1.Items.Delete(i)
else
inc(i);
end;//while
end;
begin
if ListBox1.ItemIndex<0 then//未选中记录
showmessage('请选择记录')
else//选中记录
ListBox1.Items.Delete(ListBox1.ItemIndex);
end;
begin
if ListBox1.ItemIndex<0 then//未选中记录
showmessage('请选择记录')
else//选中记录
ListBox1.Items.Delete(ListBox1.ItemIndex);
end;