• Deselect listbox item (Excel 2000 SP3)

    Author
    Topic
    #410322

    My userform has a simple single-select listbox which is populated programmatically when the form loads.
    When I click on an item, the listbox_click event will send a message asking me if I want to do something. If I say no, then nothing will happen, and I will be returned to the listbox.
    The problem is that I want to deselect the hightighted row when I return to the listbox, else if I click on a new row, two items will be highlighted. Though the newly selected item works fine, it is rather alarming for a user to see two highlighted rows.

    I try to use
    listbox.listIndex = -1, or
    listbox.selected(listindex)=false
    to make the highlight disappear without success. Instead on debugging, I found the listbox_click event was accessed a second time after the above line was executed and the listbox_click event was first exited. It didn’t access the click event a third time though (hence it isn’t recursive). I found similar posts in other sites but apparently there was no solution.

    Have I missed something or is it peculiar to Excel’s single-select listbox? Please help.

    Viewing 1 reply thread
    Author
    Replies
    • #881067

      Any change (action) to a listbox will fire its associated event code, whether the change was done through the userintrface or through VBA makes no difference.

      To avoid recursive endless event looping, add this to the code:

      (at the top)

      Dim bNoEvents as Boolean

      In any event sub:
      Private Sub control1_click()
      If bNoEvents Then Exit Sub
      bNoEvents=True
      ‘……
      bNoEvents=False
      End Sub

    • #881068

      This happens because you’re interfering with the Click event of the list box. Clicking on a list box is generally used to let the user select an item. If possible, use the DblClick event to execute the action, or provide a separate command button. That way, you may not need the message box at all.

      • #881531

        Thanks HansV. You help me solve my problem. I use the _DblClick event instead of a command button as my list is rather long. Thanks too to Pieterse for responding quickly to my query.

      • #881532

        Thanks HansV. You help me solve my problem. I use the _DblClick event instead of a command button as my list is rather long. Thanks too to Pieterse for responding quickly to my query.

    Viewing 1 reply thread
    Reply To: Deselect listbox item (Excel 2000 SP3)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: