我的界面有兩個listbox控件.listbox1是用來新增.listbox2的items是讀取數據庫的數據.現在問題是這樣的.我把listbox2的items加到listbox1中并從listbox2中刪除.如果我突然不想把listbox2其中的一個items加到listbox1中.想把它從listbox1的items除掉并在listbox2重新顯示出來.但這是在還沒有提交的時候.
我的代碼是這樣的,會出錯提示:你的listbox1.itemindex(-1)或listbox2.itemindex(-1)
procedure TForm4.Button1Click(Sender: TObject);
begin
if ListBox2.ItemIndex <> -1 then
begin
ListBox1.Items.Add(ListBox1.Items.Strings[ListBox1.ItemIndex]);
ListBox2.Items.Delete(ListBox1.ItemIndex);
end;
end;procedure TForm4.Button2Click(Sender: TObject);
begin
listbox1.Refresh;
if ListBox1.ItemIndex <> -1 then
begin
ListBox2.Items.Add(ListBox1.Items.Strings[ListBox1.ItemIndex]);
ListBox1.Items.Delete(ListBox1.ItemIndex);
end;
end;
我的代碼是這樣的,會出錯提示:你的listbox1.itemindex(-1)或listbox2.itemindex(-1)
procedure TForm4.Button1Click(Sender: TObject);
begin
if ListBox2.ItemIndex <> -1 then
begin
ListBox1.Items.Add(ListBox1.Items.Strings[ListBox1.ItemIndex]);
ListBox2.Items.Delete(ListBox1.ItemIndex);
end;
end;procedure TForm4.Button2Click(Sender: TObject);
begin
listbox1.Refresh;
if ListBox1.ItemIndex <> -1 then
begin
ListBox2.Items.Add(ListBox1.Items.Strings[ListBox1.ItemIndex]);
ListBox1.Items.Delete(ListBox1.ItemIndex);
end;
end;
出劃提示是這樣的:list index out of bounds(-1).
高手請幫忙.急用!
ListBox1.Items.Add(ListBox1.Items.Strings[ListBox1.ItemIndex]);
ListBox2.Items.Delete(ListBox1.ItemIndex);
换成这个
ListBox1.Items.Add(ListBox2.Items.Strings[ListBox2.ItemIndex]);
ListBox2.Items.Delete(ListBox2.ItemIndex);
试试