Sunday, October 23, 2011

Menambahkan Item Ke dalam ListBox tanpa Duplikat - VB6

Menggunakan fungsi API, sehingga kecepatannya bisa dikatakan sangat baik. Adapun kodenya adalah seperti di bawah ini:
Option Explicit 

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
Private Const
LB_FINDSTRINGEXACT As Long = &H1A2

Private Function
AddItemListUnix(lst As ListBox, ByVal sItem As String) As Boolean
If
(SendMessage(lst.hwnd, LB_FINDSTRINGEXACT, -1&, ByVal sItem) > -1) Then Exit Function
lst.AddItem sItem
End Function
Mengenai contoh penggunaannya:
Private Sub Command1_Click() 
AddItemListUnix List1, "Test"
AddItemListUnix List1, "Test"
AddItemListUnix List1, "Form"
AddItemListUnix List1, "Test"
AddItemListUnix List1, "CommandButton"
AddItemListUnix List1, "CommandButton"
End Sub