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

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
 
M

mangist

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)
 
G

Guest

Hi,
Try this code:

listBox1.Items.Remove(listBox1.SelectedItem);

Thanks and regards,
Manish Bafna.
MCP and MCTS.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top