Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...