Private Function Token() As String
Dim Length As Byte
Length = 10
Dim Chars As Char() = New Char() {"a"c, "b"c, "c"c, "d"c, "e"c, "f"c, _
"g"c, "h"c, "i"c, "j"c, "k"c, "l"c, _
"m"c, "n"c, "o"c, "p"c, "q"c, "r"c, _
"s"c, "t"c, "u"c, "v"c, "w"c, "x"c, _
"y"c, "z"c, "A"c, "B"c, "C"c, "D"c, _
"E"c, "F"c, "G"c, "H"c, "I"c, "J"c, _
"K"c, "L"c, "M"c, "N"c, "O"c, "P"c, _
"Q"c, "R"c, "S"c, "T"c, "U"c, "V"c, _
"W"c, "X"c, "Y"c, "Z"c, "0"c, "1"c, _
"2"c, "3"c, "4"c, "5"c, "6"c, "7"c, _
"8"c, "9"c}
Dim [Str] As String = String.Empty
Dim Random As New Random()
For a As Byte = 0 To Length - 1
[Str] += Chars(Random.[Next](0, 61))
Next
Return ([Str])
End Function
txtpassword.Text =令牌()
以上粗体线是正确的..它也不起作用
以上代码用于为应该在文本框中显示的用户生成随机密码,如果可能的话,在标签中显示更为明智的
答案 0 :(得分:2)
1)编写一些测试以查看您的Token
方法是否有效。如果您使用的是Visual Studio,甚至可以在IDE中进行测试。在return
方法的Token
语句中设置断点,然后使用 Watch 窗口查看将返回的值。
2)您的txtpassword.Text = Token()
声明似乎是正确的。这就提出了一个问题:txtpassword
是否实际上是您正在查看的表单上的控件,并且也是可见的。是吗? (如果您动态创建了txtpassword
,是否进行了myForm.Controls.Add(txtpassword)
?
(你的代码还有其他一些问题。 - 例如make Length
是一个常量,重命名变量,使它们不具有内置类型或关键字的名称,可能找到更好的方法初始化char数组等 - 但这是另一个问题。)
答案 1 :(得分:1)
使用除[String]
之外的任何其他变量名称,它是保留关键字,可能会产生问题,然后尝试。
答案 2 :(得分:1)
我刚测试了你的代码,它运行正常没有任何问题,只是我在表单上放了一个文本框和一个按钮,然后将你的函数复制/粘贴到表单类中并放入
txtpassword.Text = Token()
按钮点击...
这是完整的代码:
Public Class Form1
Private Function Token() As String
Dim Length As Byte
Length = 10
Dim Chars As Char() = New Char() {"a"c, "b"c, "c"c, "d"c, "e"c, "f"c, _
"g"c, "h"c, "i"c, "j"c, "k"c, "l"c, _
"m"c, "n"c, "o"c, "p"c, "q"c, "r"c, _
"s"c, "t"c, "u"c, "v"c, "w"c, "x"c, _
"y"c, "z"c, "A"c, "B"c, "C"c, "D"c, _
"E"c, "F"c, "G"c, "H"c, "I"c, "J"c, _
"K"c, "L"c, "M"c, "N"c, "O"c, "P"c, _
"Q"c, "R"c, "S"c, "T"c, "U"c, "V"c, _
"W"c, "X"c, "Y"c, "Z"c, "0"c, "1"c, _
"2"c, "3"c, "4"c, "5"c, "6"c, "7"c, _
"8"c, "9"c}
Dim [Str] As String = String.Empty
Dim Random As New Random()
For a As Byte = 0 To Length - 1
[Str] += Chars(Random.[Next](0, 61))
Next
Return ([Str])
End Function
Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click
TextEdit1.Text = Token()
End Sub
End Class