Results 1 to 13 of 13
  1. #1
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    414
    Thanks
    3
    Thanked 1 Time in 1 Post

    Behavior of dropdown list's TopIndex property (Wor

    I want a form's scrolling dropdown list to open with the selected entry in the middle, not at the top.

    This code should accomplish my objective by setting the list's TopIndex property to the index of the selected entry minus 1/2 the height of the list, but in any case no less than 0.

    Private Sub xxxxx_DropButtonClick()
    Dim top As Integer
    top = cbWeight.ListIndex - (xxxxx.ListRows / 2)
    If top < 0 Then top = 0
    xxxxx.TopIndex = top
    End Sub

    It doesn't work. When I try to set TopIndex I get an error which says, "Could not set the TopIndex property. Invalid property value." I get this with a value of 0, 1, or any other small number. (The list has 40 entries.)

    Does anyone know what's happening here?

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

    Re: Behavior of dropdown list's TopIndex property (Wor

    I can't find a confirmation for this (except for combo boxes in FoxPro), but I suspect that the TopIndex property of a combo box is read-only in runtime.

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Behavior of dropdown list's TopIndex property (Wor

    My guess for the reason this does not work is that the event fires before the drop-down opens. Therefore, there is no TopIndex value to manipulate while the event handler runs. You can work around this by monitoring the mouse over the drop-down:

    <pre>Private blnReTop As Boolean

    Private Sub ComboBox1_DropButtonClick()
    If Me.ComboBox1.TopIndex = -1 Then
    blnReTop = True
    Else
    blnReTop = False
    End If
    End Sub

    Private Sub ComboBox1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, _
    ByVal X As Single, ByVal Y As Single)
    If Me.ComboBox1.TopIndex <> -1 Then
    If blnReTop Then
    Me.ComboBox1.TopIndex = 5
    blnReTop = False
    End If
    End If
    End Sub</pre>

    Not pretty, but... (you have to set the flag or the user will be frustrated trying to scroll the list as it keeps recentering itself).

  4. #4
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    414
    Thanks
    3
    Thanked 1 Time in 1 Post

    Re: Behavior of dropdown list's TopIndex property

    The help page for TopIndex says that it "Sets and returns the item that appears in the topmost position in the list." That seems like a pretty clear statement that it is not supposed to be read-only.

    The MouseMove code didn't entirely make sense to me, so I wrote a more primitive version of it which was intended to clarify the sequence of events rather than actually to solve the problem:

    Private Sub xxx_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If xxx.TopIndex > -1 Then
    xxx.TopIndex = xxx.TopIndex - 1
    End If
    End Sub

    When Word stops at a breakpoint on the assignment statement the drop-down list is open, and I still get the "Invalid property value" message. Perhaps something else is wrong.

    At this point I feel that for whatever reason, VBA really doesn't want to let me do this. I wonder whether there's another method of accomplishing the desired result?

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

    Re: Behavior of dropdown list's TopIndex property

    Jefferson's method does work, although it has side effects (since the MouseMove event keeps on firing, you'll get a message when you close the form)

    See attached document.

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Behavior of dropdown list's TopIndex property

    Well, you can't convince me my code is wrong if you didn't even try it!

    Okay, pay attention: when TopIndex = -1, you CANNOT set a value, it means the value is invalid. That's why the complicated dance with the two event handlers.

    The way it works is this: when the user mouses over the control, the MouseMove event fires and checks whether it is supposed to be moving the TopIndex. The answer is no until the drop down is dropped down. In the DropButtonClick event, we set a flag so that on the next mousemove, the TopIndex is reset to the 5th item in the list. (My test had 12 items, I hardcoded a 5; you can change this part.) It then turns itself off. When the DropButtonClick event fires with the list open (i.e., the TopIndex is other than -1), it turns/keeps the flag false to avoid recentering the list as the user moves over it.

    Hans may have found a bug in my approach, but I will leave that to you to explore.

  7. #7
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Behavior of dropdown list's TopIndex property

    No message in Word XP SP2. What message are you getting?

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

    Re: Behavior of dropdown list's TopIndex property

    Hmm, I'm getting no messages now either. When I created the document last night, every time I closed the form I got a message that doing so would reset the code, but I can't reproduce it any more. I guess I had been messing around too much <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    So your code is working perfectly now, and I hope it'll work for jsachs77 too.

  9. #9
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    414
    Thanks
    3
    Thanked 1 Time in 1 Post

    Re: Behavior of dropdown list's TopIndex property

    > Well, you can't convince me my code is wrong if you didn't even try it!...
    >Hans may have found a bug in my approach, but I will leave that to you to explore.

    I'm sorry you were put off by my message. Since you stated that you hadn't tried the code yourself, and I did try a simplified version of it which duplicated the essential function, I hope you'll understand that I felt justified in expressing some doubt.

    I appreciate the help you've given me with this problem, but I've used up the time I had available to solve it (and then some). I'm going to have to set it aside for a few days. I'll report back when I've had time to work on it some more.

  10. #10
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Behavior of dropdown list's TopIndex property

    > Since you stated that you hadn't tried the code yourself...

    Maybe there's a mix-up between two different threads. I spent about a half hour on this one. I know it works. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  11. #11
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    414
    Thanks
    3
    Thanked 1 Time in 1 Post

    Re: Behavior of dropdown list's TopIndex property

    I'm sorry about the misunderstanding. You started your response with "My guess is...," and I assumed that if you had tried your suggested solution, you would not need to guess.

  12. #12
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    414
    Thanks
    3
    Thanked 1 Time in 1 Post

    Re: Behavior of dropdown list's TopIndex property

    I played with this today, and I got the following code to work:

    Dim weightCentered As Boolean

    <pre>Private Sub cbWeight_MouseMove( ... )
    Dim pos As Integer
    If cbWeight.TopIndex = -1 Then
    weightCentered = False
    ElseIf Not weightCentered Then
    weightCentered = True
    pos = cbWeight.TopIndex - cbWeight.ListRows / 2
    If pos > -1 Then
    cbWeight.TopIndex = pos
    Else
    cbWeight.TopIndex = 0
    End If
    End If
    End Sub
    </pre>


    I'm happier with this, because it requires logic in only one event handler.

  13. #13
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Behavior of dropdown list's TopIndex property

    Good work!

Posting Permissions

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