Results 1 to 4 of 4
  1. #1
    2 Star Lounger
    Join Date
    Feb 2002
    Posts
    155
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Deselect listbox item (Excel 2000 SP3)

    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.

  2. #2
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Deselect listbox item (Excel 2000 SP3)

    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
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Deselect listbox item (Excel 2000 SP3)

    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.

  4. #4
    2 Star Lounger
    Join Date
    Feb 2002
    Posts
    155
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Deselect listbox item (Excel 2000 SP3)

    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.

Posting Permissions

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