Problems with the Windows Forms Listbox (VB.NET 1.1)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have auser control with two listboxes on it. When the user selects one or
several items in the left box and clicks Add, they are copied into the
listbox on the right. When the user selects one or multiple items in the
listbox on the right (lstSelected) I want to remove the selected items.

I tried doing it like this (for one):
lstSelectedList.Items.Remove(lstSelectedList.SelectedIndex)

and it doesn't throw an error, but no items are removed.

What is wrong with my code?

Also, how do I code it so that if the user selects multiple items, they are
removed?

thanks

Philip
 
Hi,

I have auser control with two listboxes on it. When the user selects one or
several items in the left box and clicks Add, they are copied into the
listbox on the right. When the user selects one or multiple items in the
listbox on the right (lstSelected) I want to remove the selected items.

I tried doing it like this (for one):
lstSelectedList.Items.Remove(lstSelectedList.SelectedIndex)

and it doesn't throw an error, but no items are removed.

What is wrong with my code?

Also, how do I code it so that if the user selects multiple items, they are
removed?

thanks

Philip

The Remove() method expects the ListBoxItem object. You are looking
for the RemoveAt() method which takes an index of the item.

On some button click (like 'Remove') you want this code:

Button_OnClick() {
for (int i = lstSelectedItems.SelectedIndices.Count ; i >= 0; -- i )
{
lstSelectedItems.RemoveAt (lstSelectedIndices);
}
}

(in C#, my VB isn't up to scratch)
 
Hi,
Try this code:

listBox1.Items.Remove(listBox1.SelectedItem);

Thanks and regards,
Manish Bafna.
MCP and MCTS.
 
Back
Top