vb.net 防止列表框重复

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20010683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:50:51  来源:igfitidea点击:

Prevent Listbox Duplicates

vb.net

提问by user2980316

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.

好的,新手问题在这里。我正在创建一个随机密钥生成器,它将从字符串生成密钥,并将每个组合键添加到列表框。我的问题是如何防止重复项出现/添加到列表框中,从而防止重复项。目前,密钥被生成为 5 个单独的部分,然后(粗略地)在将其添加到列表框之前将其组合到一个不可见的文本框中以进行临时存储。

  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)

(请注意, Key1.text 包含一个静态值,因此所有键都以相同的方式开始。我在 .net 4.5 中使用了visual basic)

回答by Karl Anderson

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

使用列表框的Contains()方法来测试值是否已经在列表框中,像这样:

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>).

更有效的方法是使用不允许重复的 .NET 集合(即HashSet<T>)。

Per MSDN:

根据 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.

HashSet 类提供高性能的集合操作。集合是不包含重复元素且其元素没有特定顺序的集合。

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

注意:HashSet<T>.Add()方法返回 a Boolean(True如果项目已添加到集合中并且False项目已经存在)。

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