Vicky
Vicky

Reputation: 1

How to Remove Selected Items from Multiple List Boxes

I have two listboxes and listbox1 selected index changed Event has the code :

      listBox2.SelectedIndex = listBox1.SelectedIndex

so when i click the item in one listbox , the Item with the same Index is selected automatically in another listbox, now i wanna remove these two items with a button . when i write the following code :

      listBox1.Items.Remove(listBox1.SelectedItem);
      listBox2.Items.Remove(listBox2.SelectedItem);

It Deletes only the Listbox1 SelectedItem , why not it is Deleting ListBox 2 selectedItem ?? I need Urgent Help / Response .I Would be very Greatful/ThankFul to all of YOU. THANKS IN ADVANCE ,

Upvotes: 0

Views: 1574

Answers (2)

TonySalimi
TonySalimi

Reputation: 8427

Another solution is to keep the selected index of each listbox in two separate variables and then delete each item by the index stored beforehand.

Upvotes: 0

EgorBo
EgorBo

Reputation: 6142

try to delete them in another order:

  listBox2.Items.Remove(listBox2.SelectedItem);
  listBox1.Items.Remove(listBox1.SelectedItem);

When you deleted the selecteditem at the first ListBox, SelectionChanged is firing and set SelectedItem of listbox2 into null.

Upvotes: 3

Related Questions