user2980316
user2980316

Reputation: 149

Prevent Listbox Duplicates

Okay, newbie question here. I'm creating a random key generator which will generate keys from a string, and add each key combination to a Listbox. My question is how would I be able to prevent duplicates from appearing/being added to the listbox thus preventing duplicate keys. Currently the key is generated into 5 separate sections and is then (crudely) brought together into an invisible textbox for temp storage before it's added into the listbox1.

  generatetextonlycode = strName

    TextBox1.Text = Key1.Text & "-" & Key2.Text & "-" & Key3.Text & "-" & Key4.Text & "-" & Key5.Text`

I know this is a really bad way to go about it, but it's easy and works -- Only it is prone to duplicates ;( This code obviously will go into a Loop statement once it works. Here's the full thing:

Private Sub Generatebtn_Click(sender As Object, e As EventArgs) Handles Generatebtn.Click
    Key2.Text = generatetextonlycode()
    Key3.Text = generatetextonlycode()
    Key4.Text = generatetextonlycode()
    Key5.Text = generatetextonlycode()
End Sub


Public Function generatetextonlycode() As Object
    Dim intRnd As Object
    Dim strName As Object
    Dim intNameLength As Object
    Dim intLenght As Object
    Dim strInputString As Object
    Dim inStep As Object

    strInputString = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"

    intLenght = Len(strInputString)

    intNameLength = 5

    Randomize()

    strName = ""

    For inStep = 1 To intNameLength

        intRnd = Int((intLenght * Rnd()) + 1)

        strName = strName & Mid(strInputString, intRnd, 1)

    Next

    generatetextonlycode = strName

    TextBox1.Text = Key1.Text & "-" & Key2.Text & "-" & Key3.Text & "-" & Key4.Text & "-" & Key5.Text

    '
    '
    'THIS IS WHERE I'D LIKE TO ADD THE CONTENTS OF Textbox1 INTO Listbox1 IF THE LISTBOX DOESNT ALREADY CONTAIN THE KEY! 
    '
    '

End Function

(Note that Key1.text contains a static value so that all keys start of the same. I'm using visual basic with .net 4.5)

Upvotes: 2

Views: 9837

Answers (1)

Karl Anderson
Karl Anderson

Reputation: 34844

Use the list box's Contains() method to test if the value is already in the list box or not, like this:

If Not listBox1.Items.Contains(TextBox1.Text) Then
    ' It is not already in the list box so add it
    ListBox1.Items.Add(TextBox1.Text)
End If

A more efficient approach is to use a .NET collection that does not allow duplicates (i.e. HashSet<T>).

Per MSDN:

The HashSet class provides high-performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.

Note: HashSet<T>.Add() method returns a Boolean (True if the item was added to the collection and False if the item is already present).

So your code could be this:

Dim theValues As New HashSet(Of String)
Dim success As Boolean = theValues.Add(TextBox1.Text)

' Was the addition of the string successful or not?     
If success Then
    ' Yes, so re-bind the list box
End If

Upvotes: 5

Related Questions