2010-02-21 59 views
1
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 =令牌()傳遞函數的值到一個文本框

在上述粗體線正確..它不工作太

和上述代碼用於生成應在文本框中會顯示用戶的隨機密碼,如果可能的話在標籤這是更advicable

+0

你能解釋一下你的問題是什麼? – 2010-02-21 10:24:55

+0

我建議改變變量'String'的名字;使用關鍵字的變量名是非常不好的做法... – 2010-02-21 10:25:48

回答

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 
1

[String]使用任何其他變量的名字,它被保留的關鍵字,並可能造成問題,然後嘗試。

+0

s先生現在我已經添加 – 2010-02-21 10:34:38

+0

你仍然得到一個錯誤?如果是的那是什麼? – Sarfraz 2010-02-21 10:35:25

+0

s sir它不會顯示任何錯誤,但密碼值r不會顯示在文本框 – 2010-02-21 10:50:55

2

1)寫一些測試,看你的Token方法是否有效。如果你使用Visual Studio,你甚至可以在IDE中測試它。在Token方法中的return語句中設置斷點,並使用觀察窗口查看將返回的值。

2)您的txtpassword.Text = Token()聲明似乎是正確的。這提出了一個問題,txtpassword是否實際上是您正在查看的窗體上的控件,並且也是可見的。是嗎? (如果您創建txtpassword動態,你做一個myForm.Controls.Add(txtpassword)

(有在你的代碼順便說一句其他一些問題 - 如:使Length恆定,重命名你的變量,所以他們不承擔在 - 的名字建立類型或關鍵字,可能找到一個更好的方式來初始化該字符數組等 - 但這將是另一個問題。)

相關問題