Home > Visual Basic 6 > Source Code AutoComplete Pada ComboBox Di Visual Basic

Source Code 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 "Ari Hadiyono"
      .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

Jika masih belum jelas, Download Source Code AutoComplete Pada ComboBox Di Visual Basic


Referensi :
Masino Sinaga

http://www.masinosinaga.com/

About these ads
  1. August 2, 2010 at 11:13 pm

    Wah, ini nih, aku belum tau bagaimana membuat auto complit kek gini. Makasih :)

    • August 2, 2010 at 11:24 pm

      oke. sama2…

  2. mabsuthi
    August 20, 2010 at 3:09 am

    Salam kenal,
    lengkap sekali, thanks ya..
    kalau untuk mengisi combonya dengan nama field dari tabel gimana yak,,
    saya ingin membuatnya untuk form pencarian berdasarkan isi combo box diatas.

  3. yohan
    October 17, 2010 at 12:04 pm

    mau tanya kalo mencari script vb untuk cari no nis gmna ya??
    tau nggak?
    thanks ya!

  4. firman
    March 29, 2011 at 2:26 am

    salam kenal agan iram,,,
    dapet pencerahan hehe bs nya2 ne,, :)

  5. firman
    March 29, 2011 at 2:26 am

    firman :
    salam kenal agan irman,,,
    dapet pencerahan hehe bs nya2 ne,,

  6. February 6, 2012 at 7:54 pm

    Yang di cariii ada disini izin sedot ya gan…semoga tambah suksesss……amiiinnnn :)

  7. memet
    February 7, 2012 at 3:37 pm

    tnhan’k u bro

  8. memet
    February 7, 2012 at 3:39 pm

    memet :
    thank’s u bro

  1. May 18, 2012 at 8:03 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 139 other followers

%d bloggers like this: