Auto-Complete ComboBox

Auto Complete adalah teknik melengkapi teks pada ComboBox (sesuai item yang telah terdaftar pada ComboBox) berdasarkan penggalan teks yang dimasukkan.

Tambahkan sebuah ComboBox pada form!

Buatlah sebuah module, Ketik:

Option Explicit

Sub Komplit(cbo As ComboBox, Koleksi$, KeyCode As Integer)
    Dim iCtr As Long
    Dim intSelStart As Integer
    Dim Pisah() As String
    
    'Jika konstanta KeyCode di bawah 65
    'maka abaikan
    If KeyCode < 65 Then Exit Sub
    
    intSelStart = cbo.SelStart
    
    If intSelStart = 0 Then intSelStart = 1
    
    'Memecah koleksi
    Pisah = Split(Koleksi$, "||")
    
    'Pengulangan pencarian teks
    'sebanyak pecahan koleksi
    For iCtr = 0 To UBound(Pisah)
        If LCase(Left$(Pisah(iCtr), _
        Len(cbo.Text))) = LCase(cbo.Text) Then
            cbo.Text = Pisah(iCtr)
            cbo.SelStart = intSelStart
            cbo.SelLength = 255
            Exit Sub
        End If
    Next
    cbo.SelStart = intSelStart
End Sub

Buka jendela kode form, ketik:

Option Explicit

Dim KoleksiKota As String

Private Sub Combo1_KeyUp(KeyCode As Integer, Shift As Integer)
    'Memanggil prosedur Komlit
    Call Komplit(Combo1, KoleksiKota, KeyCode)
End Sub

Private Sub Form_Load()
    Dim i As Integer
    
    'Mengisi item pada Combo1
    With Me.Combo1
        .AddItem "Bandung"
        .AddItem "Bogor"
        .AddItem "Cirebon"
        .AddItem "Cimahi"
        .AddItem "Ciamis"
        .AddItem "Tasikmalaya"
        .AddItem "Jakarta"
        .AddItem "Semarang"
        .AddItem "Yogyakarta"
    End With
    
    'Mengisi daftar koleksi item
    For i = 0 To Me.Combo1.ListCount - 1
        KoleksiKota = KoleksiKota & Me.Combo1.List(i) & "||"
    Next
End Sub

Perihal Novian Agung
Pengangguran

2 Responses to Auto-Complete ComboBox

  1. ARDI mengatakan:

    MAKASIH…
    BISA..

  2. Jerry Brand mengatakan:

    Bro, gimana kalo datanya diambil dari database? thanks ya

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: