VB 2008 Help With Listbox + Remove Selected

GeForceX

Supreme [H]ardness
Joined
Mar 19, 2003
Messages
4,172
Hi guys,

I got a little thing I can't really figure out at this moment.

I have a single button that says "Remove Selected Item".
But I also have two list boxes with its own categories.

I have to be able to select something from EITHER one of the list boxes and have the "Remove Selected Item" take care of it.

There's one thing I know but it only works for one listbox at a time:

Code:
lstListBox.Items.RemoveAt(lstListBox.SelectedIndex)

How do I get that to remove from any list box?

Thanks!
 
Hi guys,

I got a little thing I can't really figure out at this moment.

I have a single button that says "Remove Selected Item".
But I also have two list boxes with its own categories.

I have to be able to select something from EITHER one of the list boxes and have the "Remove Selected Item" take care of it.

There's one thing I know but it only works for one listbox at a time:

Code:
lstListBox.Items.RemoveAt(lstListBox.SelectedIndex)

How do I get that to remove from any list box?

Thanks!

You can do something like


1 sec, I saw a error in my first reply and now my brain can't think.
 
you could add an event to get focus on the list box and use it to determine which was the last clicked on then use that to determine which to remove from?
 
since its only two the easiest is just write two lines, one for each listbox, in the event handler for your btn click
 
you could foreach the controls in the parent container, and if it's (listbox) remove the selected. But that seems like overkill.
 
Something like this perhaps.
Code:
If lstListBox.SelectedItems.Count > 0 Then
	If lstListBox.Items.RemoveAt(lstListBox.SelectedIndex)
End If
If lstListBox2.SelectedItems.Count > 0 Then
	If lstListBox2.Items.RemoveAt(lstListBox2.SelectedIndex)
End If
 
Do both ListBoxes need to have something selected simultaneously? If not then I'd wire up the SelectionChanged (or whatever it's called) event to set the other ListBox's selection to -1. In this way you can always keep track of which box was last selected, since you (presumably) only want one item selected between the two boxes.

If this makes sense, then the rest of the code is easy--the Remove button just checks to see if a ListBox has a selected index that isn't -1.... if so then remove that item from that ListBox.

2.png
 
I thought I responded to this last night. Apparently, I suck at the internet.


In the event handler, cast object sender as a listbox.

Code:
 Private Sub ListBox_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles ListBox1.SelectionChanged, ListBox2.SelectionChanged
        Dim lbSender As ListBox = sender
        lbSender.Items.Remove(lbSender.SelectedItem)
    End Sub

Then for every listbox you want to use that handler, just add the listbox name at the end of the Sub declaration after "Handles"

So
... Handles ListBox1.SelectionChanged, ListBox2.SelectionChanged, etc.
 
The code you provided will delete an entry from the list box any time its selection changes. I can't imagine that's a very usable interface. Is that really what the OP wants?
 
If you want to remove the item that is selected in the last listbox, you can find which listbox has focus. If you have an event (such as right click, with a context menu that says remove). Then you can wire up for right click to all the list boxes, then tag the context menu with the sender object (or store it in a variable).. and do... (sender as ListBox).RemoveItem etc..
 
Back
Top