Tip ini sangat berguna apabila menggunakan combobox yang di dalamnya terdiri atas ratusan bahkan ribuan data. User (pengguna/pemakai program) akan kesulitan jika harus memilih satu per satu menggunakan scroll combobox ke bawah hingga data yang diinginkan ketemu. Dengan adanya tip ini, user cukup hanya mengetikkan beberapa karakter awal, dan program akan otomatis melengkapinya, sehingga tidak perlu mengetik sampai akhir. Nah bagi yang berkenan untuk Tip ini silahkan ikuti langkah-lah berikut
- Buatlah Project Baru Standart Exe
- Tambahkan 1 buah ComboBox Style : Dropdown Combo
- Tambahkan 1 buah Module
'Ketik Coding berikut pada Module
Option Explicit
Const CB_FINDSTRING = &H14C
Public 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
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
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
' Ketik Coding diberikut pada Form
Option Explicit
Private Sub Combo1_KeyPress(KeyAscii As Integer)
KeyAscii = AutoComplete(Combo1, KeyAscii, False, Asli)
End Sub
Private Sub Form_Load()
Call AddData
End Sub
Private Sub AddData()
With Combo1
.Clear
.AddItem "Ana Lestari"
.AddItem "Budi Setiawan"
.AddItem "Eka Syahputra"
.AddItem "Wahyu Perdana"
.AddItem "Blog walking"
.AddItem "Terserah"
End With
End Sub
Option Explicit
Const CB_FINDSTRING = &H14C
Public 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
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
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
' Ketik Coding diberikut pada Form
Option Explicit
Private Sub Combo1_KeyPress(KeyAscii As Integer)
KeyAscii = AutoComplete(Combo1, KeyAscii, False, Asli)
End Sub
Private Sub Form_Load()
Call AddData
End Sub
Private Sub AddData()
With Combo1
.Clear
.AddItem "Ana Lestari"
.AddItem "Budi Setiawan"
.AddItem "Eka Syahputra"
.AddItem "Wahyu Perdana"
.AddItem "Blog walking"
.AddItem "Terserah"
End With
End Sub
Kemudian Run (jalankan) Program atau tekan F5, lalu pada combobox coba ketik huruf a , maka secara Otomatis teks pada combobox akan menjadi Ana Lestari. Semoga Tip ini ada mamfaat nya bagi kita semua, dan jangan lupa nitip komennya ya, terimakasih sebelumnya ... salam