Board index » Visual Studio » CB_SETTOPINDEX and Combobox

CB_SETTOPINDEX and Combobox

Visual Studio106
With a VB 6 Combobox (type 2) I was hoping to set the selected item to the

3rd item from the top when the dropdown is clicked. I read that the

TopIndex property doesn't work with a type 2. My first question is, does a

SendMessage CB_TopIndex call work with such a combob?. If so, then can

somebody tell me how to do it? This is what I've got:



'In the Form

Private Sub cboTimeIncrement_DropDown()

'put the time for the selected item in middle of dropdown

With Me.cboTimeIncrement

'this returns 0 so no error, I think

Debug.Print cbo_set_topindex(Me.cboTimeIncrement, IIf(.ListIndex - 2>=

0, .ListIndex - 2, 0))

End With

End Sub



'In a separate code module

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _

(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As

Any) As Long

Private Const CB_SETTOPINDEX As Long = &H15C



Public Function cbo_set_topindex(cbo As ComboBox, top_index As Long) As Long

cbo_set_topindex = SendMessage(cbo.hwnd, CB_SETTOPINDEX, top_index, 0)

End Function



Thanks,



Doug


-
 

Re:CB_SETTOPINDEX and Combobox

"Doug Glancy" <nobodyhere@replytogroup.com>escribi=F3 en el mensaje =

Quote
With a VB 6 Combobox (type 2) I was hoping to set the selected item to =

the=20

3rd item from the top when the dropdown is clicked. I read that the=20

TopIndex property doesn't work with a type 2. My first question is, =

does a=20

SendMessage CB_TopIndex call work with such a combob?. If so, then =

can=20

somebody tell me how to do it?



Both API message and intrinsic property works for the 'Style 2', the =

problem is that the list must be open before you set the property or =

send the message. This means that execution needs exit from the =

'_DropDown' event to be able to send the message, or set the property.



The easiest solution to this "problem" is enabling a 'Timer' in the =

'_DropDown' event, waiting in the '_Timer' event until the list becomes =

dropped, state which you can retrieve sending the CB_GETDROPPEDSTATE =

message to the 'ComboBox'.



For example, see the changes:



'******************

Quote
'In the Form

Private Sub cboTimeIncrement_DropDown()



Timer1.Interval =3D 10

Timer1.Enabled=3D True



Quote
End Sub



Private Sub Timer1_Timer()

Timer1.Enabled =3D False

Do Until IsCBListDropped(cboTimeIncrement.hwnd) =3D True

DoEvents

Loop

=20

With Me.cboTimeIncrement

'Set the property or send the message, is just the same

.TopIndex =3D IIf(.ListIndex - 2>=3D 0, .ListIndex - 2, 0)

'Debug.Print cbo_set_topindex(Me.cboTimeIncrement, =

IIf(.ListIndex - 2>=3D 0, .ListIndex - 2, 0))

End With

End Sub



'__________________________________

Quote
'In a separate code module

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" =

_

(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam =

As=20

Any) As Long

Private Const CB_SETTOPINDEX As Long =3D &H15C



Public Const CB_GETDROPPEDSTATE As Long =3D &H157



Quote
=20

Public Function cbo_set_topindex(cbo As ComboBox, top_index As Long) =

As Long

cbo_set_topindex =3D SendMessage(cbo.hwnd, CB_SETTOPINDEX, top_index, =

0)

End Function



Public Function IsCBListDropped(ByVal lngHWND As Long) As Boolean

IsCBListDropped =3D SendMessage(lngHWND, CB_GETDROPPEDSTATE, ByVal =

0&, ByVal 0&)

End Function

'******************





--=20

Greetings

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

( ! ) Preceding answers in Google:

=

groups.google.com/group/microsoft.public.vb.general.discussion">groups.google.com/group/microsoft.public.vb.general.discussion

( i ) Temperance in the forum:

www.microsoft.com/communities/conduct/default.mspx">www.microsoft.com/communities/conduct/default.mspx

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



-

Re:CB_SETTOPINDEX and Combobox

Vinchenzo,



That works perfectly. It took me a few minutes to undertstand that I needed

to add a timer control to the form - I've never used one before.



Thanks a lot,



Doug





"Vinchenzo vinç" <vbvinchenz0@BORRARhotmail.com>wrote in message

"Doug Glancy" <nobodyhere@replytogroup.com>escribió en el mensaje

Quote
With a VB 6 Combobox (type 2) I was hoping to set the selected item to the

3rd item from the top when the dropdown is clicked. I read that the

TopIndex property doesn't work with a type 2. My first question is, does a

SendMessage CB_TopIndex call work with such a combob?. If so, then can

somebody tell me how to do it?



Both API message and intrinsic property works for the 'Style 2', the

problem is that the list must be open before you set the property or send

the message. This means that execution needs exit from the '_DropDown' event

to be able to send the message, or set the property.



The easiest solution to this "problem" is enabling a 'Timer' in the

'_DropDown' event, waiting in the '_Timer' event until the list becomes

dropped, state which you can retrieve sending the CB_GETDROPPEDSTATE message

to the 'ComboBox'.



For example, see the changes:



'******************

Quote
'In the Form

Private Sub cboTimeIncrement_DropDown()



Timer1.Interval = 10

Timer1.Enabled= True



Quote
End Sub



Private Sub Timer1_Timer()

Timer1.Enabled = False

Do Until IsCBListDropped(cboTimeIncrement.hwnd) = True

DoEvents

Loop



With Me.cboTimeIncrement

'Set the property or send the message, is just the same

.TopIndex = IIf(.ListIndex - 2>= 0, .ListIndex - 2, 0)

'Debug.Print cbo_set_topindex(Me.cboTimeIncrement, IIf(.ListIndex -

2>= 0, .ListIndex - 2, 0))

End With

End Sub



'__________________________________

Quote
'In a separate code module

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _

(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As

Any) As Long

Private Const CB_SETTOPINDEX As Long = &H15C



Public Const CB_GETDROPPEDSTATE As Long = &H157



Quote


Public Function cbo_set_topindex(cbo As ComboBox, top_index As Long) As

Long

cbo_set_topindex = SendMessage(cbo.hwnd, CB_SETTOPINDEX, top_index, 0)

End Function



Public Function IsCBListDropped(ByVal lngHWND As Long) As Boolean

IsCBListDropped = SendMessage(lngHWND, CB_GETDROPPEDSTATE, ByVal 0&,

ByVal 0&)

End Function

'******************





--

Greetings

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

( ! ) Preceding answers in Google:

groups.google.com/group/microsoft.public.vb.general.discussion">groups.google.com/group/microsoft.public.vb.general.discussion

( i ) Temperance in the forum:

www.microsoft.com/communities/conduct/default.mspx">www.microsoft.com/communities/conduct/default.mspx

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -





-