none
How Can I Write a program to display 1000 8-character random user IDs in a text box after you click a button. RRS feed

  • Question

  • Hello, I am trying to write a program that displays 1000 8-character random user IDs in a text box after you click a button.
    So Far I've gotten the program to Display the 8-character random user IDs in the text box after I click the button but I cannot figure out how to make it generate in the textbox 1000 times more. THANK YOU IN ADVANCE

    THIS IS WHAT I HAVE:

    Public Class Form1
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    
            Dim Letters As New List(Of Integer)
            'add ASCII codes for numbers
            For i As Integer = 48 To 57
                Letters.Add(i)
            Next
            'lowercase letters
            For i As Integer = 97 To 122
                Letters.Add(i)
            Next
            'uppercase letters
            For i As Integer = 65 To 90
                Letters.Add(i)
            Next
    
            'select 8 random integers from number of items in Letters
            'then convert those random integers to characters and
            'add each to a string and display in Textbox
            Dim Rnd As New Random
            Dim SB As New System.Text.StringBuilder
            Dim Temp As Integer
            For count As Integer = 1 To 8
                Temp = Rnd.Next(0, Letters.Count)
                SB.Append(Chr(Letters(Temp)))
            Next
    
    
            TextBox1.Text = SB.ToString
    
    
        End Sub
    
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'TextBox1.Font = New Font("Arial", 14)
        End Sub
    
    
    
    
    End Class
    Sunday, October 8, 2017 10:30 PM

All replies

  • So Far I've gotten the program to Display the 8-character random user IDs in the text box after I click the button but I cannot figure out how to make it generate in the textbox 1000 times more.

    Two changes.   Firstly, put the relevant part of the code is a loop that runs from 1 to 1000,

    https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/for-next-statement

    Then append to the text box, instead of overwriting.  You will probably want to separate the numbers in some way (eg, a space) so that you can see which is which.

    https://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.appendtext(v=vs.110).aspx
    https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/operators-and-expressions/concatenation-operators

    Note that creating the new Random object should not be inside the loop - you should do that once only before the loop starts.

    • Proposed as answer by Frank L. Smith Sunday, October 8, 2017 10:47 PM
    Sunday, October 8, 2017 10:45 PM
  • do the ID's need to be unique ?

    Monday, October 9, 2017 9:33 PM
  • Hi P2DAO,

    I modify your code and want to get 100 8-character random userID in the TextBox1, if you want to get 1000, you can change 99 to 999.

     Dim Letters As New List(Of Integer)
            'add ASCII codes for numbers
            For i As Integer = 48 To 57
                Letters.Add(i)
            Next
            'lowercase letters
            For i As Integer = 97 To 122
                Letters.Add(i)
            Next
            'uppercase letters
            For i As Integer = 65 To 90
                Letters.Add(i)
            Next
    
            'select 8 random integers from number of items in Letters
            'then convert those random integers to characters and
            'add each to a string and display in Textbox
            Dim Rnd As New Random
            Dim SB As New System.Text.StringBuilder
            Dim Temp As Integer
            For i As Integer = 0 To 99
                For count As Integer = 1 To 8
                    Temp = Rnd.Next(0, Letters.Count)
                    SB.Append(Chr(Letters(Temp)))
                Next
                TextBox1.AppendText(SB.ToString & Environment.NewLine)
                SB.Clear()
            Next
    

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Friday, October 13, 2017 7:30 AM
    Tuesday, October 10, 2017 9:20 AM
    Moderator