Trucos Búsqueda incremental en un combobox

Para hacer una búsqueda incremental, tipo Access, en un combobox :

Declaramos en el formulario :

Private Const WM_SETREDRAW = &HB
Private Const KEY_A = 65
Private Const KEY_Z = 90
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long _
, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long


Y en el evento KeyUp del combobox :

Dim sComboText As String
Dim iLoop As Integer
Dim sTempString As String
Dim lReturn As Long
If KeyCode >= KEY_A And KeyCode <= KEY_Z Then
  'buscar sólo la letras entre la A y la Z
  sTempString = Combo1.Text
  If Len(sTempString) = 1 Then sComboText = sTempString
  lReturn = SendMessage(Combo1.hwnd, WM_SETREDRAW, False, 0&)
  For iLoop = 0 To (Combo1.ListCount - 1)
    If UCase((sTempString & Mid$(Combo1.List(iLoop), Len(sTempString) + 1))) = UCase(Combo1.List(iLoop)) Then
      Combo1.ListIndex = iLoop
      Combo1.Text = Combo1.List(iLoop)
      Combo1.SelStart = Len(sTempString)
      Combo1.SelLength = Len(Combo1.Text) - (Len(sTempString))
      sComboText = sComboText & Mid$(sTempString, Len(sComboText) + 1)
      Exit For
    Else
      If InStr(UCase(sTempString), UCase(sComboText)) Then
        sComboText = sComboText & Mid$(sTempString, Len(sComboText) + 1)
        Combo1.Text = sComboText
        Combo1.SelStart = Len(Combo1.Text)
      Else
        sComboText = sTempString
        End If
    End If
  Next iLoop
  lReturn = SendMessage(Combo1.hwnd, WM_SETREDRAW, True, 0&)
End If



Trucos Trucos

Visual Basic Página de Visual Basic

Página principal Página principal

www.jrubi.com