No Duplication of Item in ListBox using VB 6.0
Submitted by donbermoy on Tuesday, March 18, 2014 - 21:25.
In this tutorial, we will create a program that can filter duplication inside our ListBox when we input the same text in the TextBox.
Now, let's start this tutorial!
1.Let's start this tutorial by following the following steps in Microsoft Visual Basic 6.0: Open Microsoft Visual Basic 6.0, click Choose Standard EXE, and click Open.
2.Next, add only one Button named Command1 and labeled it as "Add Item", one TextBox named Text1 and make it as empty, and one LitBox named List1.You must design your interface like this:
3. Now put this code for your code module. This code is for Command1_Click:
- This line of code filters that if Listbox is empty and Textbox is not equal to nothing then it will add the string inputted in textbox to the listbox.
Then in the Else statment, we declared our tmpbol to be false and we have created a For Next Loop and we used tmpNum as the starting integer that is equal to 0 up to ListCount of Listbox. We deducted it by 1 because it is an array.
-The above code used to make our value of tmpBol will be false when the List Count of the listbox is not equal to our textbox and if tmpBol is true then it will prompt the user that the Text is already inside the listbox and it will exit the loop.
- Private Sub Command1_Click()
- Dim tmpBol As Boolean
- Dim tmpNum As Integer
- If List1.ListCount = 0 And Text1.Text <> "" Then
- List1.AddItem Text1.Text
- Else
- tmpBol = False
- For tmpNum = 0 To List1.ListCount - 1
- If List1.List(tmpNum) <> Text1.Text Then
- tmpBol = False
- Else
- tmpBol = True
- MsgBox (Text1.Text & "is already inside the listbox.")
- Exit For
- End If
- Next tmpNum
- If tmpBol = False And Text1.Text <> "" Then
- List1.AddItem Text1.Text
- End If
- End If
- End Sub
Explanation:
We first initialized variable tmpBol As Boolean and tmpNum As Integer.- If List1.ListCount = 0 And Text1.Text <> "" Then
- List1.AddItem Text1.Text
- If List1.List(tmpNum) <> Text1.Text Then
- tmpBol = False
- Else
- tmpBol = True
- MsgBox (Text1.Text & "is already inside the listbox.")
- Exit For
- End If
Output:
Download the source code and try it! :) For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISwareAdd new comment
- 276 views