Results 1 to 7 of 7

Thread: [RESOLVED] Pressing Enter in userform listbox

  1. #1

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Resolved [RESOLVED] Pressing Enter in userform listbox

    I have this code in my userform's listbox.

    Code:
    Private Sub lstFrom_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    
    Dim lngEntry As Long
    Dim bMatched As Boolean
    
    If KeyCode = vbKeyReturn Then
        For lngEntry = 0 To lstFrom.ListCount - 1
            If UCase(txtSearch) = UCase(lstFrom.List(lngEntry)) Then
                lstFrom.Selected(lngEntry) = True
                bMatched = True
            End If
        Next
    End If
    
    If Not bMatched Then
        MsgBox "'" & txtSearch & "' not found in the From list"
    End If
    End Sub
    I find that I have to press Enter twice in order for it to trigger the event. Any ideas why that would be? I've checked and the problem is not caused by other events firing first.

  2. #2
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,393

    Re: Pressing Enter in userform listbox

    If an Office application loses focus, it generally ignores the first mouse click or Enter key you use on it.

    As a test, add a MsgBox after the Dim statements in that Sub and see what happens on the first Enter key. It will probably be nothing.

  3. #3

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Pressing Enter in userform listbox

    In order for a message box to do anything for me on the first press of the Return key, the event would have to fire, and it doesn't. I proved that by placing a breakpoint on the sub's declaration line and the code doesn't get there until the second press.

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: Pressing Enter in userform listbox

    I find it curious, that you're trying to catch the Return-Key in a ListBox-Event.
    If i read this right, you have a TextBox with a SearchPhrase you want to lookup in the ListBox.
    Wouldn't it make more sense to catch the Return-Key in the TextBox-KeyDown?

    btw: SendMessage with LB_FINDSTRING/LB_FINDSTRINGEXACT/LB_SELECTSTRING is probably way faster than your "manual" search

    EDIT: Just saw that you're going for case-insensitive. So forget SendMessage

    Code:
    Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Dim i As Long
    Dim Match As Boolean
        Match = False     'precondition is a failure
        If KeyCode = vbKeyReturn Then
            For i = 0 To ListBox1.ListCount - 1
                If UCase(TextBox1.Text) = UCase(ListBox1.List(i)) Then
                    Match = True
                    Exit For
                End If
            Next
            If Not Match Then
                MsgBox "Not Found"
            Else
                ListBox1.ListIndex = i  'Select the Item at Index i
            End If
        End If
    End Sub
    
    Private Sub UserForm_Click()
        ListBox1.AddItem "Michael"
        ListBox1.AddItem "Adam"
        ListBox1.AddItem "John"
        ListBox1.AddItem "Mary"
        ListBox1.AddItem "Harry"
        ListBox1.AddItem "Ronald"
    End Sub
    As for "i have to hit Enter twice":
    You probably have the ListBox' TabIndex one higher than the TextBox' TabIndex
    (Textbox having TabIndex 1, the ListBox having TabIndex 2)
    IIRC, there is a textBox-setting/property, which is Default "if Return-Key is hit in Textbox, jump to the Next control in TabIndex-Order" (basically having the "same" function as the Tab-Key),
    and since you're trying to catch the ReturnKey in the ListBox, it's no wonder you have to hit Return twice.
    You are inside the TextBox
    You hit Return
    Since you have no code catching the ReturnKey inside a TextBox-Event it behaves as described above: It jumps to the next Control, which is your ListBox
    You hit Return again
    Now, since the ListBox has Focus, your KeyDown-Event is evaluated

    EDIT2: Found it: https://docs.microsoft.com/en-us/off...avior-property
    Last edited by Zvoni; Jun 10th, 2022 at 03:25 AM.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  5. #5

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Pressing Enter in userform listbox

    Wow, am I embarrassed. The KeyDown event was in the wrong place. It should have been in the code for the 'Search' textbox on the userform that I want to use to locate entries in a listbox with many entries. I put the KeyDown event in the textbox and now it works properly. Thank you Zvoni for pointing out my error.

    Code:
    Private Sub txtSearch_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    
    Dim lngEntry As Long
    Dim bMatched As Boolean
    If KeyCode = vbKeyReturn Then
        For lngEntry = 0 To lstFrom.ListCount - 1
            If UCase(txtSearch) = UCase(lstFrom.List(lngEntry)) Then
                lstFrom.Selected(lngEntry) = True
                bMatched = True
            End If
        Next
    Else
        Exit Sub
    End If
    
    If Not bMatched Then
        MsgBox "'" & txtSearch & "' not found in the From entries"
    End If
    End Sub

  6. #6
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: Pressing Enter in userform listbox

    Martin,
    be aware that with your current code, in case of duplicate entries in the listbox you would get the last Match selected!
    If you want to select the first match, or if the entries in the Listbox are unique (no duplicates) then Exit the For-Loop after the Match (see my code above)
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  7. #7

Tags for this Thread

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