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.)

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:

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).

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.

Re: Behavior of dropdown list's TopIndex property

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.

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.