0
好的,新手問題在這裏。我創建一個隨機密鑰生成器,它將從一個字符串生成密鑰,並將每個密鑰組合添加到一個列表框。我的問題是我將如何能夠防止重複出現/被添加到列表框,從而防止重複鍵。目前,密鑰被生成爲5個獨立的部分,然後(粗暴地)將其添加到不可見的文本框中以進行臨時存儲,然後將其添加到listbox1中。防止列表框重複
generatetextonlycode = strName
TextBox1.Text = Key1.Text & "-" & Key2.Text & "-" & Key3.Text & "-" & Key4.Text & "-" & Key5.Text`
我知道這是一個非常糟糕的方式去了解它,但它很容易和作品 - 只有很容易出現重複;(此代碼顯然將進入循環語句,一旦它的工作原理這裏的。全事情:
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
(請注意,Key1.text包含一個靜態值,使所有鍵啓動相同的我使用的Visual Basic和.NET 4.5。)