AutoComplete Pada ComboBox Di Visual Basic

Source code berikut untuk membuat AutoComplete pada ComboBox di Visual Basic. Dengan mengetikkan beberapa huruf awal saja dan jika terdapat dalam list ComboBox tersebut maka kita tidak perlu menulis sisa dari kata yang dimaksud. Untuk lebih jelasnya,


Buat 1 project dengan :
1 Form
5 ComboBox

Copy source code berikut pada Form :
Const CB_FINDSTRING = &H14C

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

Public Enum EnumKarakter
  Asli = 0
  Ubah = 1
End Enum
Dim Karakter As EnumKarakter

Private Sub IsiSemuaCombobox()
  Dim ctrl As Control
  For Each ctrl In Form1.Controls
    If TypeOf ctrl Is ComboBox Then
      With ctrl
      .AddItem "Irman Firmansyah"
      .AddItem "Irwine Darwin"
      .AddItem "Mikey Chuck"
      .AddItem "Ady Chandra"
      .AddItem "Nassroom Minallah"
      .AddItem "Dorce Simatupang"
      .AddItem "Darah Muda"
      .Text = .List(0)
      End With
    End If
  Next
End Sub

Private Sub Form_Load()
  IsiSemuaCombobox
End Sub

Private Sub Combo1_KeyPress(KeyAscii As Integer)
  KeyAscii = AutoComplete(Combo1, KeyAscii, True, Asli)
End Sub
Private Sub Combo4_KeyPress(KeyAscii As Integer)
  KeyAscii = AutoComplete(Combo4, KeyAscii, False, Asli)
End Sub

Private Sub Combo2_KeyPress(KeyAscii As Integer)
  KeyAscii = AutoComplete(Combo2, KeyAscii, False, Ubah)
End Sub

Private Sub Combo3_KeyPress(KeyAscii As Integer)
  KeyAscii = AutoComplete(Combo3, KeyAscii, True, Ubah)
End Sub

Private Sub Combo5_KeyPress(KeyAscii As Integer)
  KeyAscii = AutoComplete(Combo5, KeyAscii)
End Sub

Public Function AutoComplete( _
                cbCombo As ComboBox, _
                sKeyAscii As Integer, _
                Optional bUpperCase As Boolean = True, _
                Optional cCharacter As EnumKarakter = Asli) _
                As Integer
  Dim lngFind As Long, intPos As Integer
  Dim intLength As Integer, tStr As String
  With cbCombo
    If sKeyAscii = 8 Then
       If .SelStart = 0 Then Exit Function
       .SelStart = .SelStart - 1
       .SelLength = 32000
       .SelText = ""
    Else
       intPos = .SelStart
       tStr = .Text
       If bUpperCase = True Then
          .SelText = UCase(Chr(sKeyAscii))
       Else
          .SelText = (Chr(sKeyAscii))
       End If
    End If
    lngFind = SendMessage(.hwnd, CB_FINDSTRING, 0, ByVal .Text)
    If lngFind = -1 Then
       .Text = tStr
       .SelStart = intPos
       .SelLength = (Len(.Text) - intPos)
       AutoComplete = 0
       Exit Function
    Else
       intPos = .SelStart
       intLength = Len(.List(lngFind)) - Len(.Text)
       If cCharacter = Ubah Then
         .SelText = .SelText & Right(.List(lngFind), intLength)
       Else
         .Text = .List(lngFind)
       End If
       .SelStart = intPos
       .SelLength = intLength
    End If
  End With
End Function


DOWNLOAD Full Source Project
Comments
0 Comments