locked
resetting listbox selectedindex when user clicks blank area inside listbox RRS feed

  • Question

  • Hi all,

    Whenever the user clicks the blank area below the last item in a listbox. I need to reset the selectedindex to -1.

    (instead of being returned the last selectedindex by the indexchanged event).

    My questions are:

    1. What is the best way to do this (or, How to detect a click in the *blank area* . Then I can manually set the index to -1.)?

    2. Why does the selectedindexchanged event fire, when the the index has not changed when I clicked the blank area of the listbox ?

     

    Thank You !

    Thursday, September 16, 2010 5:35 AM

Answers

  • yes If I understood your question the right way this is absolutely possible, I would use something like this:

     

     Private Sub ListBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseClick

            If (ListBox1.IndexFromPoint(e.X, e.Y) = ListBox.NoMatches) Then

                ListBox1.SelectedIndex = -1

            End If

        End Sub

     

     

    Gr. Ryan.

    PS. I don't know the answer on you second question srry.

     

    Oh credits to: Vbnet12322 on forum http://itknowledgeexchange.techtarget.com were I found the solution once!


    The nerd way of life: Instead of learning for your exams, you should hack into the school's computer and get your answers, that's by far the best way to spend your time!:P
    • Proposed as answer by Chao KuoModerator Monday, September 20, 2010 7:59 AM
    • Marked as answer by VB-Zapped Monday, September 20, 2010 8:01 PM
    Saturday, September 18, 2010 11:47 AM

All replies

  • Wow, It's 4 days now & no one's responded.

    So, is this impossible to do in VB 2010 ?

     

    Saturday, September 18, 2010 7:18 AM
  • 1.

      Private Sub ListBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseClick
        Dim LB As ListBox = CType(sender, ListBox)
        If e.Location.Y > LB.Items.Count * LB.ItemHeight Then
          LB.SelectedIndex = -1
        End If
      End Sub
    
    

    2.  Just surmising.  The selectedindexchanged event needs something to happen to trigger it.  That something could be a new item getting focus - but what happens when no item gets the focus, how would the event be triggered then.  So maybe it occurs when an item loses focus.  If you look at a listbox when you click on the blank area you will see that the focus moves to the last item - so maybe that is what triggers the selectedindex change.

    Saturday, September 18, 2010 11:08 AM
  • yes If I understood your question the right way this is absolutely possible, I would use something like this:

     

     Private Sub ListBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseClick

            If (ListBox1.IndexFromPoint(e.X, e.Y) = ListBox.NoMatches) Then

                ListBox1.SelectedIndex = -1

            End If

        End Sub

     

     

    Gr. Ryan.

    PS. I don't know the answer on you second question srry.

     

    Oh credits to: Vbnet12322 on forum http://itknowledgeexchange.techtarget.com were I found the solution once!


    The nerd way of life: Instead of learning for your exams, you should hack into the school's computer and get your answers, that's by far the best way to spend your time!:P
    • Proposed as answer by Chao KuoModerator Monday, September 20, 2010 7:59 AM
    • Marked as answer by VB-Zapped Monday, September 20, 2010 8:01 PM
    Saturday, September 18, 2010 11:47 AM
  • Hi Dave, Ryan

    Many thanks for your kind examples.

     

    Dave,

    Referring to your answer to query 2, that is not what happens.

    When I click the blank area below the list box, the last item *does not* get selected. The highlight persists on the previous item that was selected.

    If you check the selected index, it does not change when the blank area is clicked.

    So, (I believe) the selected index changed event should not be triggered.

    Additionally, I notice that if an already selected item(s) is clicked again, the event still triggers, though the selection has not changed.

    Though this is behaviour is not at all a serious issue, it does seem a wee bit inefficient.

    Thanks to all !

     

     


    Monday, September 20, 2010 8:32 PM
  • When I click the blank area below the list box, the last item *does not* get selected.
     I didn't say it did.  I said it gets the focus (as indicated by the dotted rectangle round the item).  If you use the Up key after clicking you will see that the selected item then changes to the last item and moves up from there, indicating that the focus had changed.
    Monday, September 20, 2010 8:54 PM