如果不能添加重复项,如下
private void AddItem_Click(object sender, EventArgs e)
{
string addItem = textBox1.Text.Trim();
if (!string.IsNullOrEmpty(addItem) && !listBox1.Items.Contains(addItem))
{
listBox1.Items.Add(addItem);
}
}
private void RemoveItem_Click(object sender, EventArgs e)
{
string removeItem = textBox1.Text.Trim();
if (!string.IsNullOrEmpty(removeItem) && listBox1.Items.Contains(removeItem))
{
listBox1.Items.Remove(removeItem);
}
}
如果可以添加重复项,如下
添加:把AddItem_Click中!listBox1.Items.Contains(addItem)删了
删除:如果想一个一个删就不改,如果想一起删就改成:
private void RemoveItem_Click(object sender, EventArgs e)
{
string removeItem = textBox1.Text.Trim();
if (!string.IsNullOrEmpty(removeItem))
{
while (listBox1.Items.Contains(removeItem))
{
listBox1.Items.Remove(removeItem);
}
}
}