如果不能新增重複項,如下
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)刪了
if (!string.IsNullOrEmpty(removeItem))
while (listBox1.Items.Contains(removeItem))
如果不能新增重複項,如下
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);
}
}
}