comboBox - Set ListIndex Problem

comboBox - Set ListIndex Problem

Post by AK » Wed, 09 Aug 2006 14:16:10


I am trying to set the .ListIndex property of a combobox to the index
corresponding to an item in the list by utilizing the string value of the
indexed item.


For example, I know that I can set the item displayed to the first one by
setting the list index to 0, as follows,


With cboRecipientName

.AddItem "Name1"
.AddItem "Name2"
.AddItem "Name3"

End With

.ListIndex = 0




What I would like to do is as follows

With cboRecipientName

.AddItem "String1"
.AddItem "String2"
.AddItem "String3"

End With


'Psuedo Code to set the item displayed
cboRecipientName.ListIndex = ListIndex of item whose string value is
"String3"


So, I guess in general, I am asking for how to return the ListIndex of an
item by using the item's string value.

Thanks

AK
 
 
 

comboBox - Set ListIndex Problem

Post by erewho » Wed, 09 Aug 2006 16:38:58


Option Explicit

Private Sub Command1_Click()
Combo1.AddItem "Fred"
Combo1.AddItem "Joe"
Combo1.AddItem "Bert"

Combo1.ListIndex = LF_GetListIndex(Combo1, "Joe")

End Sub

Private Function LF_GetListIndex(Combo As ComboBox, S$) As Integer
Dim L9&

For L9 = 0 To Combo.ListCount - 1
If S$ = Combo.List(L9) Then
LF_GetListIndex = L9
Exit Function
End If
Next
LF_GetListIndex = -1
End Function

 
 
 

comboBox - Set ListIndex Problem

Post by NickH » Wed, 09 Aug 2006 17:05:15

Can't you not just set the .Text property.
With cboRecipientName
.AddItem "String1"
.AddItem "String2"
.AddItem "String3"
.Text = "String3"
End With

Depending on the value of .Style this may error if the string is not in the
list.

NickHK
 
 
 

comboBox - Set ListIndex Problem

Post by AK » Thu, 10 Aug 2006 00:04:14

Nick,

I thought that the text property of a combobox was read only?


I will try your idea too, though. Maybe my error many moons ago when I tried
to set the text property was indeed because I was trying to set it to a
value that was not in the list already.

AK
 
 
 

comboBox - Set ListIndex Problem

Post by AK » Thu, 10 Aug 2006 00:37:33

Oh My! Thanks a bunch.


AK
 
 
 

comboBox - Set ListIndex Problem

Post by Mike » Thu, 10 Aug 2006 05:14:42


It's only read-only if the combobox's style is set to 2-Dropdown list. Even
so, if the string assigned to the Text property exactly matches an item in
the combobox, that item will be selected (identical to assigning a value to
the ListIndex property). You just need to make sure you've got an active
error handler to trap the possible error.

Other than this specific scenario, the VB-only way would be to loop through
all the items until you find the matching string.

There is an API-way too that, IMO, is the best method of all 3 of these.

-----BEGIN CODE
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 LB_FINDSTRING = &H18F
Private Const LB_FINDSTRINGEXACT = &H1A2
Private Const CB_FINDSTRING = &H14C
Private Const CB_FINDSTRINGEXACT = &H158

Public Function FindListBoxItemByString(oListBox As Control, SearchString As
String, Optional ExactMatch As Boolean = True, Optional StartingIndex As
Long = -1) As Long

' Purpose: Uses the Win32 API to quickly find a listbox
' item matching a specified string. Returns
' the ListIndex value, or -1 if no match found.
' Can be used for both listboxes and comboboxes.
'

Dim lMsg As Long
Dim lRet As Long

'Determine which message to send based on type of control and ExactMatch
param

Select Case LCase$(TypeName(oListBox))
Case "listbox", "cslistbox"
If ExactMatch Then
lMsg = LB_FINDSTRINGEXACT
Else
lMsg = LB_FINDSTRING
End If
Case "combobox", "cscombobox"
If ExactMatch Then
lMsg = CB_FINDSTRINGEXACT
Else
lMsg = CB_FINDSTRING
End If
Case Else
Err.Raise 513, , "ListBox or ComboBox control required to be
passed to FindListBoxItemByString"
End Select

FindListBoxItemByString = SendMessage(oListBox.hwnd, lMsg,
StartingIndex, ByVal SearchString)

End Function
-----END CODE

The above function will work with either a listbox or combobox, as well as
with the Cresent controls of these.

Here's an extremely simple usage example:

Private Sub Form_Click()

Combo1.ListIndex = FindListBoxItemByString(Combo1, Text1.Text)

End Sub

Private Sub Form_Load()

Text1.Text = ""
With Combo1
.AddItem "Ann"
.AddItem "Eddie"
.AddItem "Sophie"
.AddItem "Al"
.AddItem "Steve"
End With

End Sub

Play around with calling it with the 2 optional parameters. For example,
you could expand on this simple example by say, typing "a" in the textbox
and "toggling" between "Ann" and "Al" with each click on the form. I've
leave that as an exercise for you. <g>

--
Mike
Microsoft MVP Visual Basic