多行中的随机字符串生成器

时间:2018-05-04 14:05:20

标签: vb.net

如何在多行中创建多个随机字符串?例如:

waee
wefe
sety
rtgs

这是我的代码:

Public Class Form3
    Dim pool As String = ""

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        pool = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ._"

        Dim count = 0
        TextBox1.Text = ""

        Dim cc As New Random
        Dim strpos = ""

        While count <= TextBox2.Text
            strpos = cc.Next(0, pool.Length)
            TextBox1.Text = TextBox1.Text & pool(strpos)
            count = count + 1
        End While
    End Sub

End Class

1 个答案:

答案 0 :(得分:0)

要做的第一件事是确保TextBox1Mutliline属性设置为True

要做的第二件事是检查Options声明。有迹象表明此文件设置为Option StrictOption Infer以外的其他内容,但这并不好。如果count <= TextBox2.Text没有导致编译错误,那么就会出现问题。

然后你可以这样做:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim pool As String = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ._"
    Dim cc As New Random()

    Dim Length As Integer = CInt(TextBox2.Text) - 1
    Dim result(Length) As Char
    For count As Integer = 0 To Length
        result(count) = pool(cc.Next(0, pool.Length))
    Next

    TextBox1.Text &= (New String(result)) & vbCrLf
End Sub