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?
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
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...
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
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
Damn! I always forget you can do that! <g>
> 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
> > 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.
"Norm Cook" <norm...@cableone.net> wrote in message
news:vht1ggg...@corp.supernews.com...