micropathic Posted June 6, 2004 Posted June 6, 2004 Hi, I am trying to remove multiple selected items from a listbox and I came up with this code which I thought would would work correctly, but doesn't: Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click Dim j As Integer For Each j In ListBox1.SelectedIndices ListBox1.Items.RemoveAt(j) Next End Sub Any help would be very appreciated Quote
ozie Posted June 7, 2004 Posted June 7, 2004 try this Do While ListBox1.SelectedItems.Count > 0 ListBox1.Items.Remove(ListBox1.SelectedItem) Loop Oz Quote
micropathic Posted June 7, 2004 Author Posted June 7, 2004 That worked great, Thanks for your help! Quote
micropathic Posted June 7, 2004 Author Posted June 7, 2004 Just noticed something with that code though, if you have the same item in a row above the one you are removing, it will remove that as well. For instance if this is how my listbox looked: 4 7 2 2 2 2 2 2 <--if removed this, it would also remove the five rows above it 2 So, I would end up with: 4 7 2 So here's what I got to work just in case anyone ends up searching for something like this in the future, which is a bit of what I tried to begin with, mixed with the code posted by ozie: Do While ListBox1.SelectedItems.Count > 0 ListBox1.Items.RemoveAt(ListBox1.SelectedIndex) Loop Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.