Presented by Redjkibersama.blogspot.com - Silahkan download sepuasnya, dijamin gratis..tis..tis... !

Friday, April 8, 2011

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 : 
01Const CB_FINDSTRING = &H14C
02 
03Private 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
07 
08Public Enum EnumKarakter
09  Asli = 0
10  Ubah = 1
11End Enum
12Dim Karakter As EnumKarakter
13 
14Private Sub IsiSemuaCombobox()
15  Dim ctrl As Control
16  For Each ctrl In Form1.Controls
17    If TypeOf ctrl Is ComboBox Then
18      With ctrl
19      .AddItem "Irman Firmansyah"
20      .AddItem "Irwine Darwin"
21      .AddItem "Mikey Chuck"
22      .AddItem "Ady Chandra"
23      .AddItem "Ari Hadiyono"
24      .AddItem "Dorce Simatupang"
25      .AddItem "Darah Muda"
26      .Text = .List(0)
27      End With
28    End If
29  Next
30End Sub
31 
32Private Sub Form_Load()
33  IsiSemuaCombobox
34End Sub
35 
36Private Sub Combo1_KeyPress(KeyAscii As Integer)
37  KeyAscii = AutoComplete(Combo1, KeyAscii, True, Asli)
38End Sub
39Private Sub Combo4_KeyPress(KeyAscii As Integer)
40  KeyAscii = AutoComplete(Combo4, KeyAscii, False, Asli)
41End Sub
42 
43Private Sub Combo2_KeyPress(KeyAscii As Integer)
44  KeyAscii = AutoComplete(Combo2, KeyAscii, False, Ubah)
45End Sub
46 
47Private Sub Combo3_KeyPress(KeyAscii As Integer)
48  KeyAscii = AutoComplete(Combo3, KeyAscii, True, Ubah)
49End Sub
50 
51Private Sub Combo5_KeyPress(KeyAscii As Integer)
52  KeyAscii = AutoComplete(Combo5, KeyAscii)
53End Sub
54 
55Public Function AutoComplete( _
56                cbCombo As ComboBox, _
57                sKeyAscii As Integer, _
58                Optional bUpperCase As Boolean = True, _
59                Optional cCharacter As EnumKarakter = Asli) _
60                As Integer
61  Dim lngFind As Long, intPos As Integer
62  Dim intLength As Integer, tStr As String
63  With cbCombo
64    If sKeyAscii = 8 Then
65       If .SelStart = 0 Then Exit Function
66       .SelStart = .SelStart - 1
67       .SelLength = 32000
68       .SelText = ""
69    Else
70       intPos = .SelStart
71       tStr = .Text
72       If bUpperCase = True Then
73          .SelText = UCase(Chr(sKeyAscii))
74       Else
75          .SelText = (Chr(sKeyAscii))
76       End If
77    End If
78    lngFind = SendMessage(.hwnd, CB_FINDSTRING, 0, ByVal .Text)
79    If lngFind = -1 Then
80       .Text = tStr
81       .SelStart = intPos
82       .SelLength = (Len(.Text) - intPos)
83       AutoComplete = 0
84       Exit Function
85    Else
86       intPos = .SelStart
87       intLength = Len(.List(lngFind)) - Len(.Text)
88       If cCharacter = Ubah Then
89         .SelText = .SelText & Right(.List(lngFind), intLength)
90       Else
91         .Text = .List(lngFind)
92       End If
93       .SelStart = intPos
94       .SelLength = intLength
95    End If
96  End With
97End Function
Jika masih belum jelas.., Download Source Code AutoComplete Pada ComboBox Di Visual Basic

Referensi :
Masino Sinaga
http://www.masinosinaga.com/

No comments:

Daftar Isi