-
Hey, this should be baby stuff, but I'm missing something. If I type a value into a combobox, have the code select a value and I then check the listindex, it's -1 instead of the selected value's index:
Code:
Private Sub Form_Load()
With Combo1
.AddItem "1"
.AddItem "2"
.AddItem "3"
.AddItem "4"
End With
Private Sub Combo1_Change() 'fired when typing - not click
'the value's not actually hard-coded
'it selects the index if the typed value matches one of the list entries
'but this simple hard-coding is done for example b/c it doesn't work either
Combo1.ListIndex = 3
End Sub
Private Sub Command1_Click()
MsgBox Combo1.ListIndex 'keeps returning -1
End Sub
Command1 keeps returning -1. If you type a value into the combo, it picks the 2nd item, but outside of that combobox, nothing recognizes this.
Thanks!
-
try this
Private Sub Form_Load()
With Combo1
.AddItem "1"
.AddItem "2"
.AddItem "3"
.AddItem "4"
End With
End Sub
Private Sub Command1_Click()
MsgBox Combo1.Text 'keeps returning -1
End Sub
HansZ
-
But I don't need the text. I need the listindex --- the control is later validated for things like having a valid listindex.
-
Try:
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 CB_FINDSTRING = &H14C
Private Sub Form_Load()
Dim lIndex As Long
For lIndex = 1 To 4
Combo1.AddItem lIndex
Next
Combo1 = ""
End Sub
Private Sub Combo1_Validate(Cancel As Boolean)
Dim sText As String
sText = Combo1
Combo1.ListIndex = SendMessage(Combo1.hwnd, CB_FINDSTRING, 0&, ByVal sText)
End Sub
Private Sub Command1_Click()
Caption = Combo1.ListIndex
End Sub
-
-
Actually what I had works if I put in the validate event -- though yours is more efficient. Is there a way to make this work in the keypress event so that the user doesn't have to tab away to trigger this processing (if a name is found, it populates another control which I'd like to have happen as soon as the user finishes typing a valid name)?
Thanks.