No Duplication of Item in ListBox using VB 6.0

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: design 3. Now put this code for your code module. This code is for Command1_Click:
  1. Private Sub Command1_Click()
  2.  
  3. Dim tmpBol As Boolean
  4. Dim tmpNum As Integer
  5.  
  6. If List1.ListCount = 0 And Text1.Text <> "" Then
  7. List1.AddItem Text1.Text
  8. Else
  9. tmpBol = False
  10. For tmpNum = 0 To List1.ListCount - 1
  11. If List1.List(tmpNum) <> Text1.Text Then
  12. tmpBol = False
  13. Else
  14. tmpBol = True
  15. MsgBox (Text1.Text & "is already inside the listbox.")
  16. Exit For
  17. End If
  18. Next tmpNum
  19. If tmpBol = False And Text1.Text <> "" Then
  20. List1.AddItem Text1.Text
  21. End If
  22. End If
  23. End Sub

Explanation:

We first initialized variable tmpBol As Boolean and tmpNum As Integer.
  1. If List1.ListCount = 0 And Text1.Text <> "" Then
  2. List1.AddItem Text1.Text
- 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.
  1. If List1.List(tmpNum) <> Text1.Text Then
  2. tmpBol = False
  3. Else
  4. tmpBol = True
  5. MsgBox (Text1.Text & "is already inside the listbox.")
  6. Exit For
  7. End If
-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.

Output:

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/BermzISware

Add new comment