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 :
01 | Const CB_FINDSTRING = &H14C |
03 | Private Declare Function SendMessage Lib "user32" _ |
04 | Alias "SendMessageA" ( ByVal hwnd As Long , _ |
05 | ByVal wMsg As Long , ByVal wParam As Long , _ |
06 | lParam As Any) As Long |
08 | Public Enum EnumKarakter |
12 | Dim Karakter As EnumKarakter |
14 | Private Sub IsiSemuaCombobox() |
16 | For Each ctrl In Form1.Controls |
17 | If TypeOf ctrl Is ComboBox Then |
19 | .AddItem "Irman Firmansyah" |
20 | .AddItem "Irwine Darwin" |
21 | .AddItem "Mikey Chuck" |
22 | .AddItem "Ady Chandra" |
23 | .AddItem "Ari Hadiyono" |
24 | .AddItem "Dorce Simatupang" |
32 | Private Sub Form_Load() |
36 | Private Sub Combo1_KeyPress(KeyAscii As Integer ) |
37 | KeyAscii = AutoComplete(Combo1, KeyAscii, True , Asli) |
39 | Private Sub Combo4_KeyPress(KeyAscii As Integer ) |
40 | KeyAscii = AutoComplete(Combo4, KeyAscii, False , Asli) |
43 | Private Sub Combo2_KeyPress(KeyAscii As Integer ) |
44 | KeyAscii = AutoComplete(Combo2, KeyAscii, False , Ubah) |
47 | Private Sub Combo3_KeyPress(KeyAscii As Integer ) |
48 | KeyAscii = AutoComplete(Combo3, KeyAscii, True , Ubah) |
51 | Private Sub Combo5_KeyPress(KeyAscii As Integer ) |
52 | KeyAscii = AutoComplete(Combo5, KeyAscii) |
55 | Public Function AutoComplete( _ |
56 | cbCombo As ComboBox, _ |
57 | sKeyAscii As Integer , _ |
58 | Optional bUpperCase As Boolean = True , _ |
59 | Optional cCharacter As EnumKarakter = Asli) _ |
61 | Dim lngFind As Long , intPos As Integer |
62 | Dim intLength As Integer , tStr As String |
65 | If .SelStart = 0 Then Exit Function |
66 | .SelStart = .SelStart - 1 |
72 | If bUpperCase = True Then |
73 | .SelText = UCase(Chr(sKeyAscii)) |
75 | .SelText = (Chr(sKeyAscii)) |
78 | lngFind = SendMessage(.hwnd, CB_FINDSTRING, 0, ByVal .Text) |
82 | .SelLength = (Len(.Text) - intPos) |
87 | intLength = Len(.List(lngFind)) - Len(.Text) |
88 | If cCharacter = Ubah Then |
89 | .SelText = .SelText & Right(.List(lngFind), intLength) |
91 | .Text = .List(lngFind) |
94 | .SelLength = intLength |
Jika masih belum jelas..,
Download Source Code AutoComplete Pada ComboBox Di Visual Basic
—
Referensi :
Masino Sinaga
http://www.masinosinaga.com/
No comments:
Post a Comment