0

I have come here asking for help on how to make my string generator generate the amount of strings typed in TextBox2. For example, if I typed 10 in the box it would generate 10 strings in the RichTextBox1 and if I typed 1 it would generate 1 etc. Here is my code.

 Public Function RandomString(ByVal length As Integer) As String
    Dim strb As New System.Text.StringBuilder
    Dim chars() As String = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"} 

    Dim UpperBound As Integer = UBound(chars)

    For x As Integer = 1 To length
        strb.Append(chars(Int(Rnd() * UpperBound)))
    Next

    Return strb.ToString

End Function

Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
    Try
        System.Diagnostics.Process.Start("Link Removed...")
    Catch
    End Try
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim rndstring As String
    rndstring = RandomString(24)
    RichTextBox1.Text = rndstring
End Sub
haws1290
  • 105
  • 1
  • 9

2 Answers2

0

What you need to do is modify your click handler for Button1 to repeat the number of times specified in TextBox2. However, you should verify that the user specified a valid numeric value in the text box as well. Something like this:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim repeatCnt As Integer

    'Check for valid numeric entry
    If Integer.TryParse(TextBox2.Text, repeatCnt) Then
        For repeatIdx As Integer = 1 To repeatCnt
            Dim rndstring As String
            'Generate random string...
            rndstring = RandomString(24)
            '...and append to text box with a line break
            TextBox1.Text &= rndstring & vbCrLf
        Next
    Else
        MessageBox.Show("Please enter a valid integer number in the text box")
    End If
End Sub

An extra suggestion might be to change the text box that the user specifies the repeat value to a NumericUpDown control.

Also note that, for large repeat values, the append to the text box Text property would not be efficient due to string immutability so perhaps another StringBuilder would be appropriate.

Community
  • 1
  • 1
Bob Mc
  • 1,980
  • 1
  • 28
  • 38
0

You could use this method (basically it's just an additional For-loop):

Private Shared rnd As New Random()
Private Shared chars() As String = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}

Public Shared Function RandomStrings(ByVal length As Int32, count As Int32) As IEnumerable(Of String)
    Dim builder = New System.Text.StringBuilder()
    Dim strings = New List(Of String)
    For c As Int32 = 1 To count
        For l As Int32 = 1 To length
            builder.Append(chars(rnd.Next(0, chars.Length)))
        Next
        strings.Add(builder.ToString())
        builder.Clear()
    Next
    Return strings
End Function

create 100 random strings with length 10:

Dim allStrings = RandomStrings(10, 100)
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939