VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Autotype Combo Box

by Shon (1 Submission)
Category: Custom Controls/Forms/Menus
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (4 Votes)

This code was taken from O'Neil. It searches a combo box as the user types. O'Neil's code was modified to use the SendMessage API to search the combo box, which made it much faster. This is very fast, even with thousands of records in the combo box. Thank you O'Neil for the idea, and the well commented code!

API Declarations
Public 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

Public Const CB_FINDSTRING = &H14C

Rate Autotype Combo Box

Private Sub Combo1_Change()
  Dim i As Integer
  Dim l As Long
  Dim strNewText As String
  ' Check to see if a search is required.
  If Not IgnoreTextChange And Combo1.ListCount > 0 Then
    l = SendMessage(Combo1.hwnd, CB_FINDSTRING, -1, ByVal CStr(Combo1.Text))
    strNewText = Combo1.List(l)
    If Len(Combo1.Text) <> Len(strNewText) Then
      ' Partial match found
      ' Avoid recursively entering this event
      IgnoreTextChange = True
      i = Len(Combo1.Text)
      ' Attach the full text from the list to what has
      ' already been entered. This technique preserves
      ' the case entered by the user.
      Combo1.Text = Combo1.Text & Mid$(strNewText, i + 1)
      ' Select the text that is auto-entered
      Combo1.SelStart = i
      Combo1.SelLength = Len(Mid$(strNewText, i + 1))
    End If
  Else
    ' The IgnoreTwextChange Flag is only effective for one
    ' Changed event.
    IgnoreTextChange = False
  End If
End Sub

Private Sub Combo1_GotFocus()
  ' Select existing text on entry to the combo box
  Combo1.SelStart = 0
  Combo1.SelLength = Len(Combo1.Text)
End Sub

Private Sub Combo1_KeyDown(KeyCode As Integer, Shift As Integer)
  ' If a user presses the "Delete" key, then the selected text
  ' is removed.
  If KeyCode = vbKeyDelete And Combo1.SelText <> "" Then
    ' Make sure that the text is not automatically re-entered
    ' as soon as it is deleted
    IgnoreTextChange = True
    Combo1.SelText = ""
    KeyCode = 0
  End If
End Sub

Private Sub Combo1_KeyPress(KeyAscii As Integer)
  ' If a user presses the "Backspace" key, then the selected text
  ' is removed. Autosearch is not re-performed, as that would only
  ' put it straight back again.
  If KeyAscii = 8 Then
    IgnoreTextChange = True
    If Len(Combo1.SelText) Then
      Combo1.SelText = ""
      KeyAscii = 0
    End If
  End If
  'if user presses enter, select the listindex
  If KeyAscii = 13 Then
    Combo1.ListIndex = SendMessage(Combo1.hwnd, CB_FINDSTRING, -1, ByVal CStr(Combo1.Text))
  End If
End Sub

Download this snippet    Add to My Saved Code

Autotype Combo Box Comments

No comments have been posted about Autotype Combo Box. Why not be the first to post a comment about Autotype Combo Box.

Post your comment

Subject:
Message:
0/1000 characters