Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Pre-Selecting Items in a Combo Box

0 views
Skip to first unread message

Matthew Shaw

unread,
Jul 23, 2003, 6:41:18 AM7/23/03
to
Basically what I need my program to do is check the value of a string and
pre select the item matching that value in a combo box (style 2), the items
and their positions never change. I could just use a case statement and do
(in formload):

Select Case strSkill
Case "Strength"
cboSkill.ListIndex = 0
Case "Endurance"
cboSkill.ListIndex = 1
Case Else
'Do nothing
End Select

The problem is that there are 22 different options and I want to minimise
the amount of code needed. I thought that it might be possible to select the
item in the combo box according to it's list value is this possible, or can
anyone suggest another way to do this?


Larry Serflaten

unread,
Jul 23, 2003, 8:39:20 AM7/23/03
to
"Matthew Shaw" <mat...@bigpond.net.au> wrote

Assuming they are all unique items and that their uniquesness is evident
within the first 3 characters (or use the value where that is true) you can
select the index based on matching its position in a string:

<air code>
Match = "strend" 'That's where you stopped above....
Find = LCase$(Left$("Endurance", 3))
Index = Instr(Match, Find) \ 3
If Index Then cboSkill.ListIndex = Index - 1

"Strength" would make a Find string of "str" and match the first 3 characters,
"Endurance" would use "end" and match the next 3 characters, and so on.
If 3 isn't enough to gaurentee uniqueness, then use 4 or 5, or whatever is needed,
adding spaces to those that come up short. Finding the InStr index and dividing
by the length you use will indicate where in the combobox that Match string
is, provided the Match string and Combobox always agree. To that end,
you might build the Match string from the actual items in the combo box,
but aside from that, as you see above, you can find the match in 4-5 lines
of code.

HTH
LFS


Norm Cook

unread,
Jul 23, 2003, 8:56:46 AM7/23/03
to
Perhaps: (air code)

Dim i as Long
For i = 0 to cbo.ListCount - 1
If cbo.List(i) = strSkill Then
cbo.ListIndex = i
Exit For
End if
Next

If you prefer API,

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal
lParam As Any) As Long
Const CB_FINDSTRING As Long = &H14C
cbo.ListIndex = SendMessage(cbo.hwnd, CB_FINDSTRING, -1, ByVal strSkill)

"Matthew Shaw" <mat...@bigpond.net.au> wrote in message
news:eZtIvbQU...@TK2MSFTNGP12.phx.gbl...

Bob Butler

unread,
Jul 23, 2003, 9:05:02 AM7/23/03
to
"Matthew Shaw" <mat...@bigpond.net.au> wrote in message
news:eZtIvbQU...@TK2MSFTNGP12.phx.gbl

you can loop through the list
for x=0 to cboSkill.ListCount-1
if strcomp(cboskill.list(x),strSkill,vbtextcompare)=0 then
cboskill.listindex=x
end if
end if

or you can find it with the API:
Private Const CB_FINDSTRINGEXACT = &H158 ' ComboBox search constants
Private Const CB_FINDSTRING = &H14C
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Any, ByRef lParam As Any) As Long

Public Sub ComboSelect(ByVal TheCombobox As ComboBox, ByVal TheText As
String)
Dim x As Long
x = SendMessage(TheCombobox.hwnd, CB_FINDSTRINGEXACT, -1&, ByVal TheText)
If x >= 0 Then TheCombobox.ListIndex = x
End Sub

ComboSelect cboSkill,strSkill

Rick Rothstein

unread,
Jul 23, 2003, 9:37:30 AM7/23/03
to

If the options don't change, then you know what they all are. Simply assign
the one you want to the ComboBox's Text property and that item will be
selected.

Combo1.Text = "Endurance"

Rick - MVP


Bob Butler

unread,
Jul 23, 2003, 9:51:48 AM7/23/03
to
"Rick Rothstein" <rickNOS...@NOSPAMcomcast.net> wrote in message
news:#6k5P#RUDHA...@tk2msftngp13.phx.gbl
<cut>
> Combo1.Text = "Endurance"

Damn! I always forget you can do that! <g>

Larry Serflaten

unread,
Jul 23, 2003, 10:15:00 AM7/23/03
to
"Rick Rothstein" <rickNOS...@NOSPAMcomcast.net> wrote

> If the options don't change, then you know what they all are. Simply assign
> the one you want to the ComboBox's Text property and that item will be
> selected.
>
> Combo1.Text = "Endurance"

For Style 2 - DropDown, Text is supposedly ReadOnly at runtime, and I
recently (about 2 months ago) had to work around it . I added the
work-around because an error popped up, but now I see it works!

I hate it when that happens!
<g>
LFS

Jeff Johnson [MVP: VB]

unread,
Jul 23, 2003, 11:41:29 AM7/23/03
to

"Larry Serflaten" <serf...@usinternet.com> wrote in message
news:%236sLaSS...@TK2MSFTNGP10.phx.gbl...

> > If the options don't change, then you know what they all are. Simply
assign
> > the one you want to the ComboBox's Text property and that item will be
> > selected.
> >
> > Combo1.Text = "Endurance"
>
> For Style 2 - DropDown, Text is supposedly ReadOnly at runtime, and I
> recently (about 2 months ago) had to work around it . I added the
> work-around because an error popped up, but now I see it works!

When setting the Text property of a style 2 combo, the text you set must
match an entry in the list EXACTLY. It gives me enough of the willies that I
NEVER use it that way, although I'm not opposed to READING it.


Matthew Shaw

unread,
Jul 24, 2003, 9:40:55 AM7/24/03
to
Thank you, that worked perfectly, did just the trick.


"Norm Cook" <norm...@cableone.net> wrote in message
news:vht1ggg...@corp.supernews.com...

0 new messages