Masih seputar dengan listbox di visual basic. kali ini dengan menggunakan anti duplicate. kebanyakan kita mengentry data sudah pasti banyak
kesamaan nama dalam satu form. untuk menghindarkan kesalahan kedepannya, kamu harus mensortir data satu persatu.
kalo ada 1000 entry sementara deadline tinggal besok, ngetik saja masih 11 jari, trus gimana??bikin aja langsung surat pengunduran diri.
hehehehehehehehehe.....becanda
oke serius. disini saya akan memberikan solusinya. pertama kamu harus buat desain form sama persis ya dengan saya...
listing code dan penjelasannya:
'-----------------------------------------------------------------------
Option Explicit
'penggunaan sub bernama mastrcompanyname. dengan banyak 1-20.
Private mastrCompanyNames(1 To 20) As String
'------------------------------------------------------------------------
Private Sub Form_Load()
'------------------------------------------------------------------------
mastrCompanyNames(1) = "Aston Martin"
mastrCompanyNames(2) = "Bang & Olufsen"
mastrCompanyNames(3) = "Coca-Cola"
mastrCompanyNames(4) = "Colgate-Palmolive"
mastrCompanyNames(5) = "Ford Motor Company"
mastrCompanyNames(6) = "Gulfstream Aerospace "
mastrCompanyNames(7) = "Honeywell"
mastrCompanyNames(8) = "IKEA"
mastrCompanyNames(9) = "Mozilla Foundation"
mastrCompanyNames(10) = "Nabisco"
mastrCompanyNames(11) = "NCR Corporation"
mastrCompanyNames(12) = "Nestlé"
mastrCompanyNames(13) = "Oracle"
mastrCompanyNames(14) = "Pepsi"
mastrCompanyNames(15) = "Red Hat"
mastrCompanyNames(16) = "Rolls-Royce"
mastrCompanyNames(17) = "Sun Microsystems"
mastrCompanyNames(18) = "Suzuki"
mastrCompanyNames(19) = "Taco Bell"
mastrCompanyNames(20) = "Volkswagen"
'randomize itu proses pengacakan. yang menyebabkan banyak data yang sama
Randomize
End Sub
'------------------------------------------------------------------------
Private Sub cmdLoadList_Click()
'------------------------------------------------------------------------
Dim intX As Integer
lstRandomItems.Clear
For intX = 1 To 100
lstRandomItems.AddItem mastrCompanyNames(GetRandomNumber(1, 20))
Next
End Sub
'------------------------------------------------------------------------
Private Sub cmdRemoveDups_Click()
'------------------------------------------------------------------------
RemoveListBoxDuplicates lstRandomItems
End Sub
'------------------------------------------------------------------------
Private Function GetRandomNumber(pintLowerBound As Integer, _
pintUpperBound As Integer) _
As Integer
'------------------------------------------------------------------------
' fungsi ini akan kembali acak dengan batasan
' dari kedua argument tersebut
GetRandomNumber = Int((pintUpperBound - pintLowerBound + 1) * Rnd + pintLowerBound)
End Function
'-----------------------------------------------------------------------------
Public Sub RemoveListBoxDuplicates(pobjLB As ListBox)
'-----------------------------------------------------------------------------
'menghapus duplicate yang sama pada listbox
Dim intI As Integer
Dim intJ As Integer
With pobjLB
For intI = 0 To .ListCount - 1
For intJ = .ListCount To (intI + 1) Step -1
If .List(intJ) = .List(intI) Then
.RemoveItem intJ
End If
Next
Next
End With
End Sub