[英]Removing an Item from listbox via FindByText Method
當用戶單擊復選框時,它會將項目添加到列表框中,而當用戶取消選中它時,必須將其從列表中刪除。 我正在嘗試使用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#從列表框中刪除項目
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.