TComboBox 和 TListBox 项目删除?
TComboBox and TListBox item Removal?
我正在尝试从 TEdit
中给出的文本向 TListBox
和 TComboBox
添加项目
我的代码在 TListBox
TComboBox
中添加项目时工作正常,但是当我尝试从自身和 TComobBox
中删除 TListBox
中的选定项目时,它显示访问冲突。
以下是我的代码中的程序:-
procedure TMainForm.Button1Click(Sender: TObject);
begin
ListBox1.Items.Add(Edit1.Text);
ComboBox1.Items.Add(Edit1.Text);
end;
procedure TMainForm.Button2Click(Sender: TObject);
begin
ListBox1.Items.Delete(ListBox1.Selected.Index);
ComboBox1.Items.Delete(ComboBox1.Items.IndexOf(ListBox1.Selected.Text));
end;
已解决:现在已解决小孩子的错误。这是工作代码:
procedure TMainForm.Button2Click(Sender: TObject);
begin
ComboBox1.Items.Delete(ComboBox1.Items.IndexOf(ListBox1.Selected.Text));
ListBox1.Items.Delete(ListBox1.Selected.Index);
end;
procedure TMainForm.Button2Click(Sender: TObject);
begin
if ListBox1.Selected.Index > -1 then ListBox1.Items.Delete(ListBox1.Selected.Index);
if ComboBox1.ItemIndex > - 1 then ComboBox1.Items.Delete(ComboBox1.ItemIndex);
end;
一种安全的删除方法是
procedure TForm1.DeleteItems(const TextToFind : String);
var
i1,
i2 : Integer;
begin
i1 := ListBox1.Items.IndexOf(TextToFind);
i2 := ComboBox1.Items.IndexOf(TextToFind);
if i1 >=0 then
ListBox1.Items.Delete(i1);
if i2 >=0 then
ComboBox1.Items.Delete(i2);
end;
用法:
DeleteItems(Edit1.Text);
因为这不会假设在两个列表中选择了哪些项目。
我让您使用调试器找出您获得 AV 的原因。让你自己去发现比我告诉你更有启发性。
此行从列表框中删除项目
ListBox1.Items.Delete(ListBox1.Selected.Index);
此行试图从组合框中删除项目
ComboBox1.Items.Delete(ComboBox1.Items.IndexOf(ListBox1.Selected.Text));
但是你在里面提到了ListBox1.Selected.Text。
这是指您刚刚在第一次删除中删除的项目。
交换执行顺序应该可行:
begin
ComboBox1.Items.Delete(ComboBox1.Items.IndexOf(ListBox1.Selected.Text));
ListBox1.Items.Delete(ListBox1.Selected.Index);
end
我正在尝试从 TEdit
中给出的文本向 TListBox
和 TComboBox
添加项目
我的代码在 TListBox
TComboBox
中添加项目时工作正常,但是当我尝试从自身和 TComobBox
中删除 TListBox
中的选定项目时,它显示访问冲突。
以下是我的代码中的程序:-
procedure TMainForm.Button1Click(Sender: TObject);
begin
ListBox1.Items.Add(Edit1.Text);
ComboBox1.Items.Add(Edit1.Text);
end;
procedure TMainForm.Button2Click(Sender: TObject);
begin
ListBox1.Items.Delete(ListBox1.Selected.Index);
ComboBox1.Items.Delete(ComboBox1.Items.IndexOf(ListBox1.Selected.Text));
end;
已解决:现在已解决小孩子的错误。这是工作代码:
procedure TMainForm.Button2Click(Sender: TObject);
begin
ComboBox1.Items.Delete(ComboBox1.Items.IndexOf(ListBox1.Selected.Text));
ListBox1.Items.Delete(ListBox1.Selected.Index);
end;
procedure TMainForm.Button2Click(Sender: TObject);
begin
if ListBox1.Selected.Index > -1 then ListBox1.Items.Delete(ListBox1.Selected.Index);
if ComboBox1.ItemIndex > - 1 then ComboBox1.Items.Delete(ComboBox1.ItemIndex);
end;
一种安全的删除方法是
procedure TForm1.DeleteItems(const TextToFind : String);
var
i1,
i2 : Integer;
begin
i1 := ListBox1.Items.IndexOf(TextToFind);
i2 := ComboBox1.Items.IndexOf(TextToFind);
if i1 >=0 then
ListBox1.Items.Delete(i1);
if i2 >=0 then
ComboBox1.Items.Delete(i2);
end;
用法:
DeleteItems(Edit1.Text);
因为这不会假设在两个列表中选择了哪些项目。
我让您使用调试器找出您获得 AV 的原因。让你自己去发现比我告诉你更有启发性。
此行从列表框中删除项目
ListBox1.Items.Delete(ListBox1.Selected.Index);
此行试图从组合框中删除项目
ComboBox1.Items.Delete(ComboBox1.Items.IndexOf(ListBox1.Selected.Text));
但是你在里面提到了ListBox1.Selected.Text。 这是指您刚刚在第一次删除中删除的项目。 交换执行顺序应该可行:
begin
ComboBox1.Items.Delete(ComboBox1.Items.IndexOf(ListBox1.Selected.Text));
ListBox1.Items.Delete(ListBox1.Selected.Index);
end