通过FindByText方法从列表框中删除项目
本文关键字:删除项目 列表 FindByText 方法 通过 | 更新日期: 2023-09-27 18:26:36
当用户单击复选框时,它会将项目添加到列表框中,而当用户取消选中它时,它必须从列表中删除。我正在尝试使用FindByText方法,但它似乎没有出现在我的visual studio中。这是我目前的工作:
if (checkBox1.Checked == true)
{
listBox1.Items.Add(checkBox1.Text);
}
else
{
listBox1.Items.Remove(listBox1.Items.FindByText(checkBox1.Text));
}
您添加了String
,因此也应该删除String
;所以你不需要任何FindByText
:
listBox1.Items.Remove(checkBox1.Text);
完整片段:
if (checkBox1.Checked) // "== true" is redundant
listBox1.Items.Add(checkBox1.Text);
else
listBox1.Items.Remove(checkBox1.Text);
甚至
// To prevent double adding
listBox1.Items.Remove(checkBox1.Text);
if (checkBox1.Checked)
listBox1.Items.Add(checkBox1.Text);
要在列表框中按文本查找项目,必须遍历所有项目
请参阅这篇关于从列表框中删除项目的文章:C#从列表框中删除项目