Try something like this.
var listBox1 = new ListBox();
var listBox2 = new ListBox();
for (int i = 0; i < listBox1.Items.Count; i++)
{
int index = listBox2.Items.IndexOf(listBox1.Items[i]);
if (index != -1)
{
listBox2.Items.RemoveAt(index);
}
}
You can't use
foreach
with list boxes, so use
for
.
IndexOf
finds the item in the second list box. Items start at 0, and -1 means the item was not found. You could also instead use
Items.Contains
to see if the item is in the second list box.