Results 1 to 8 of 8

Thread: Listbox item select and deselect event

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    11

    Listbox item select and deselect event

    Hi guys,

    I have a listbox which can be multi-selected. By default, when the user clicks on an item, it is selected. And when the user clicks on the same item again, it is deselected.

    Instead of the SelectedIndexChanged event which fires when an item is selected and deselected, I want to determine whether the item is selected or deselected.

    Is there such event in vb?

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    Re: Listbox item select and deselect event

    Instead of the SelectedIndexChanged event which fires when an item is selected and deselected, I want to determine whether the item is selected or deselected.
    The very first thing you should do before asking that question is look up the documentation. I Googled "vb.net ListBox events" and it led me to the ListBox Events MSDN documentation.

    After reviewing the list of events for the ListBox I did not find an event that fires when an item is selected or deselected.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    11

    Re: Listbox item select and deselect event

    Quote Originally Posted by dday9 View Post
    The very first thing you should do before asking that question is look up the documentation. I Googled "vb.net ListBox events" and it led me to the ListBox Events MSDN documentation.

    After reviewing the list of events for the ListBox I did not find an event that fires when an item is selected or deselected.
    Yes i did tried to google, but I only managed to find this from MSDN
    http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx

    Didn't understand the code, that's why I asked. Thanks for the reply though, so I guess I have try another way.

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Listbox item select and deselect event

    That event is for WPF programs and not related to Windows Forms applications, which is probably why you didn't understand the code. There is no other event in a Windows Forms ListBox that determent exactly which item that has been selected or unselected except from the SelectedXXXChanged events. You need to use the SelectedIndexChanged and loop through the SelectedItems collection to see which item(s) that is/are selected.
    Last edited by Joacim Andersson; Oct 3rd, 2014 at 12:50 PM.

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    Re: Listbox item select and deselect event

    You need to use the SelectedIndexChanged and loop through the SelectedItems collection to see which item(s) that is/are selected.
    This does bring up a point that I didn't think of before. You could create a class that inherits a ListBox with a custom events to signal which item was selected/deselected. The only thing that could probably trip it up is the fact that you can select multiple items in a ListBox, but even then it could still be managed.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    11

    Re: Listbox item select and deselect event

    Quote Originally Posted by Joacim Andersson View Post
    That event is for WPF programs and not related to Windows Forms applications, which is probably why you didn't understand the code. There is no other event in a Windows Forms ListBox that determent exactly which item that has been selected or unselected except from the SelectedXXXChanged events. You need to use the SelectedIndexChanged and loop through the SelectedItems collection to see which item(s) that is/are selected.
    Quote Originally Posted by dday9 View Post
    This does bring up a point that I didn't think of before. You could create a class that inherits a ListBox with a custom events to signal which item was selected/deselected. The only thing that could probably trip it up is the fact that you can select multiple items in a ListBox, but even then it could still be managed.
    Alright I will figure that out, will ask questions if I have doubts again.

    Now I have another problem, my listbox items are taken from an arraylist. When I click on the clear button, I want to remove the selected items from the array. So here goes:
    Code:
    Private Sub btnClearSel_Click(sender As Object, e As EventArgs) Handles btnClearSel.Click
    Dim index As Integer
     For Each index In listPoles.SelectedIndices
                polesArrayX.RemoveAt(index)
            Next
    End Sub
    It works if I only select 1 item, and I get this error if I selected more than 1 "An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

    Additional information: Index was out of range. Must be non-negative and less than the size of the collection."

    Any idea what went wrong?

  7. #7
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Listbox item select and deselect event

    Yes, let's suppose that you have 10 items in your listbox and subsequently you also have 10 items in your array. They are index from 0 to 9. Now you select the first and the last item in the listbox (index 0 and 9) what do you think will happen when you go into the loop you posted above? You remove the first item from the array and then you want to remove the item with index 9, but since you've already removed one item there are only 9 left which are now indexed 0 to 8, so index 9 will cause an index out of range exception.

    The solution is to loop backwards, to first remove the last item before you remove the first. So you can't use a for each loop. You need a regular for loop.
    Code:
    For i As Integer = listPoles.SelectedIndicies.Count - 1 To 0 Step -1
      polesArrayX.RemoveAt(listPoles.SelectedIndicies(i))
    Next

  8. #8

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    11

    Re: Listbox item select and deselect event

    Quote Originally Posted by Joacim Andersson View Post
    Yes, let's suppose that you have 10 items in your listbox and subsequently you also have 10 items in your array. They are index from 0 to 9. Now you select the first and the last item in the listbox (index 0 and 9) what do you think will happen when you go into the loop you posted above? You remove the first item from the array and then you want to remove the item with index 9, but since you've already removed one item there are only 9 left which are now indexed 0 to 8, so index 9 will cause an index out of range exception.

    The solution is to loop backwards, to first remove the last item before you remove the first. So you can't use a for each loop. You need a regular for loop.
    Code:
    For i As Integer = listPoles.SelectedIndicies.Count - 1 To 0 Step -1
      polesArrayX.RemoveAt(listPoles.SelectedIndicies(i))
    Next
    Awesome, that works! Didn't think of it, thank you sir!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width