Results 1 to 6 of 6

Thread: Combo doesn't hold coded selection

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363
    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!
    Wade

  2. #2
    Junior Member
    Join Date
    Feb 2000
    Location
    Netherlands
    Posts
    27

    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363
    But I don't need the text. I need the listindex --- the control is later validated for things like having a valid listindex.
    Wade

  4. #4
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    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

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363

    Wink

    Thanks Aaron!
    Wade

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363
    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.
    Wade

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width