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!
 

Bigbacon

Fully [H]
Joined
Jul 12, 2007
Messages
19,596
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.
 

Yakyb

Limp Gawd
Joined
Mar 13, 2008
Messages
474
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?
 

maxedoutcc

Gawd
Joined
Jan 26, 2001
Messages
1,001
since its only two the easiest is just write two lines, one for each listbox, in the event handler for your btn click
 

Pwyl_The_Destroyer

Limp Gawd
Joined
Aug 22, 2005
Messages
139
you could foreach the controls in the parent container, and if it's (listbox) remove the selected. But that seems like overkill.
 

Snowknight26

Supreme [H]ardness
Joined
May 8, 2005
Messages
4,333
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
 

Tytalus

Supreme [H]ardness
Joined
Nov 1, 2006
Messages
4,257
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
 

mmtom

Limp Gawd
Joined
Jan 9, 2003
Messages
319
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.
 

mikeblas

[H]ard|DCer of the Month - May 2006
Joined
Jun 26, 2004
Messages
12,776
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?
 

aL Mac

Gawd
Joined
Jul 20, 2002
Messages
949
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..
 
Top