+ Reply to Thread
Results 1 to 11 of 11

Listbox refresh problem -- mysterious phantom highlighting

  1. #1
    Registered User
    Join Date
    10-26-2010
    Location
    Bellevue,WA, USA
    MS-Off Ver
    Excel 2003
    Posts
    5

    Listbox refresh problem -- mysterious phantom highlighting

    I'm using Excel2003 VBA, and I have a single-select listbox which, for example, I populate with 4 items:
    listbox1.additem "ITEM 1"
    listbox1.additem "ITEM 2"
    listbox1.additem "ITEM 3"
    listbox1.additem "ITEM 4"

    The listbox1_click event subroutine shows:
    if listbox1 = "ITEM 3" then
    DISPLAY2
    endif

    The DISPLAY2 routine has
    listbox1.clear
    listbox1.listindex = -1 (might be redundant, I think, since I cleared it?)
    listbox1.additem "NEW ITEM 1"
    listbox1.additem "NEW ITEM 2"
    listbox1.additem "NEW ITEM 3"
    listbox1.additem "NEW ITEM 4"

    For some reason, when I select ITEM 3 in the first display, and the second display comes up with the new 4 items, it has NEW ITEM 3 highlighted. I've tried putting the listbox1.listindex = -1 or listbox1.listindex = null below the additem entries, and I've tried changing the listbox1.visible = false before populating it, and listbox1.visible = true after populating it (thinking that a repaint might fix this), but I cannot get it to display the NEW ITEM screen with nothing highlighted. Can you see what I'm doing wrong, or a workaround to this? My best workaround I could think of was to create a listbox2 for the NEW ITEM list, and make the original listbox invisible and the new one visible after I switch, but I don't see why I can't re-use the same listbox, other than this highlighting problem. (It seems to me that it may be detecting a mouse event when it goes to DISPLAY2, and thereby highlighting the area where my mouse is still hovering, but if that's it, I can't think of a way to bypass this situation.) I've also utilized a text box to display the current LISTINDEX value, and strangely, when the macro displays the NEW ITEM list, and it highlights the 3rd item, the listindex displays properly as "-1", although based on what it's highlighting, it should be showing "2".

    Thanks!

    Full Example:
    Please Login or Register  to view this content.
    Last edited by RexKatWa; 10-26-2010 at 09:41 PM. Reason: Added Code Tags

  2. #2
    Registered User
    Join Date
    10-26-2010
    Location
    Bellevue,WA, USA
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Listbox refresh problem -- mysterious phantom highlighting

    PS: I also inserted a dummy MSGBOX statement after last item in DISPLAY2, and voila! -- problem solved! But obviously I don't want to have a message pop up everytime I run that routine, just to avoid the phantom highlighting -- it's worse than the original problem!

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Listbox refresh problem -- mysterious phantom highlighting

    Hello RexKatWa,

    This is happens by design. When the cursor is placed inside the ListBox and the Click event fires, it determines which row the cursor is on and highlights it.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Registered User
    Join Date
    10-26-2010
    Location
    Bellevue,WA, USA
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Listbox refresh problem -- mysterious phantom highlighting

    Well, yes, but I've now reloaded the listbox from scratch, with new items, and on top of that, I've issued a "clear" command. If the listindex is set to "-1" there should be no highlighting on the listbox, plus I haven't technically "clicked" on the refreshed listbox, I clicked on the listbox in its first format, not after the reload, so don't see how that can be by design. If it is, how do I display the second version of the listbox without highlighting without some artificial means, like making the user click a button or something to clear it? I just want it to flow from one version of the listbox to the next, so it lets the user find items in a "list within list" procedure.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Listbox refresh problem -- mysterious phantom highlighting

    Hello RexKatWa,

    I don't have time to fully explain how controls and their events operate to you, but I do know what I am talking about. If you shift the focus away from the ListBox, as you did when displaying the message box, it will not highlight the item in the ListBox.

    It is possible to alter the behaviour of a control on a UserForm using SubClassing, but this technique is very advanced and requires extensive use of the Windows API. I am not really sure what effect you want to accomplish. However, it will most likely involve the use of other control.

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,971

    Re: Listbox refresh problem -- mysterious phantom highlighting

    Is there a reason for reusing the same listbox? It doesn't appear to give the user much option to change their mind if the click the wrong thing by mistake.
    Everyone who confuses correlation and causation ends up dead.

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Listbox refresh problem -- mysterious phantom highlighting

    Hello RexKatWa,

    Add this event code to your UserForm and it work the way you want.
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    10-26-2010
    Location
    Bellevue,WA, USA
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Listbox refresh problem -- mysterious phantom highlighting

    Thank you, Leith! Your final response is what I was looking for. (Your intermediate answer earlier today had me in the process of composing a nasty response as to your arrogance at implying that I didn't understand controls, and hinting at esoteric "solutions" that really wouldn't have sufficed. But you came through at the last minute, and saved yourself from my savage tongue-lashing. I'm sure you are relieved at that!!!)

    I take it that it was, as I suspected all along, a bug in VBA's timing, at least insofar as my particular design. (To the Romper's comment, I could certainly have created a separate listbox, and I was just about to do that as my workarond, but my vision of this application was that the user would only see one listbox at a time, and work their way through a logical heierarchy of links from one menu to another, until they reach the item they want, similar to a menuing system--I guess this may not have been obvious from my abbreviated sample program. I was admittedly stubborn in thinking that I should be able to use one listbox for this purpose and not have to swap listboxes after each iteration.) Leith, your solution works perfectly, and will be something to hold onto, if I even come across any other similar timing situations where Microsoft hasn't anticipated my bizarre turns of mind. So, Thank You, again!

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Listbox refresh problem -- mysterious phantom highlighting

    Hello RexKatWa,

    The reason the delay works is that Windows allows a maximum 20 milliseconds for a thread to complete and return control back to the message pump. Since the Application, in this case the UserForm, is responsible for handling the ListBox messages, it reports back that the ListBox has not responded. The line will be highlighted only if a message is received in time.

  10. #10
    Registered User
    Join Date
    10-26-2010
    Location
    Bellevue,WA, USA
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Listbox refresh problem -- mysterious phantom highlighting

    Good to know the background for this. Thanks!

  11. #11
    Registered User
    Join Date
    12-29-2011
    Location
    Muimbai
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Listbox refresh problem -- mysterious phantom highlighting

    Thanks Leith Ross and RexKatWa. I have same problem as you mentioned above. Now it is solve after reading you post

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1