Results 1 to 7 of 7

Thread: [RESOLVED] random number to text box

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    182

    Resolved [RESOLVED] random number to text box

    hi i found this code around the net but cant figure out how to make the values be displayed in a text box, currently the data is displayed in a list box.

    Code:
    Randomize   
    Dim X As Integer
    For X = 1 To 50
        lstNumbers.AddItem Int(Rnd() * 59 + 1)
        If X = 50 Then  
            Exit For
        End If
    Next X
    End Sub

  2. #2
    Addicted Member VB<3er's Avatar
    Join Date
    Jun 2008
    Location
    VBForums
    Posts
    133

    Re: random number to text box

    First set multiline property of ur textbox to true then add this code to whatever event u want
    Code:
    Randomize   
    Dim X As Integer
    For X = 1 To 50
        Text1.Text = Int(Rnd() * 59 + 1) & vbNewLine
        If X = 50 Then  
            Exit For
        End If
    Next X
    End Sub
    Hope it helps!! Bye!
    REMEMBER if i've been useful RATE me plz

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    182

    Re: random number to text box

    for some reason it only lists one random number. but i wanted it to loop until it reaches 50

  4. #4
    Addicted Member VB<3er's Avatar
    Join Date
    Jun 2008
    Location
    VBForums
    Posts
    133

    Re: random number to text box

    Oops sorry i edited and ****ed it up xD
    corrected:
    Code:
    Randomize
    Dim X As Integer
    For X = 1 To 50
        Text1.SelText = Int(Rnd() * 59 + 1) & vbNewLine
        If X = 50 Then
            Exit For
        End If
    Next X
    End Sub
    Btw it will start where the caret is... if u want it to be always at the bottom use this one instead:

    Code:
    Randomize
    Dim X As Integer
    For X = 1 To 50
        Text1.Text = Text1.Text & Int(Rnd() * 59 + 1) & vbNewLine
        If X = 50 Then
            Exit For
        End If
    Next X
    End Sub
    Hope it helps!
    REMEMBER if i've been useful RATE me plz

  5. #5
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: random number to text box

    Not sure why the Exit For exists in previous posts. I added a command button. Take a look at this:
    Code:
    Const ListSize = 50, MaxNum = 59
    
    Private Sub Command1_Click()
    Text1.Text = vbNullString
    Randomize
    For I = 1 To ListSize
        Text1.Text = Text1.Text & Int(Rnd * MaxNum) + 1 & vbNewLine
    Next
    End Sub
    
    Private Sub Form_Load()
    Command1.Value = True
    End Sub
    The above code clears the text box each time the command button is pressed.
    Doctor Ed

  6. #6
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: random number to text box

    Ranomize should be used only once.

    A revived version of Code_Doc's: use a String array to store numbers then Join them with vbNewLine. That run faster and Text1 does not have an extra vbNewLine at the end.
    Code:
    Option Explicit
    
    Const ListSize = 50, MaxNum = 59
    
    Private Sub Command1_Click()
       Dim sNum(1 To ListSize) As String, i As Long
       
       For i = 1 To ListSize
           sNum(i) = Int(Rnd * MaxNum) + 1
       Next
       Text1 = Join(sNum, vbNewLine)
    End Sub
    
    Private Sub Form_Load()
       Randomize
       Command1_Click
    End Sub
    Notes to OP that the numbers listed may not be unique.
    If you want they are unique some extra treatment will be required.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  7. #7
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: random number to text box

    Here's a slight modification to the anhn's code above (who modified mine):
    Code:
    Const ListSize = 50, MaxNum = 59
    Dim sNum() As String
    
    Private Sub Command1_Click()
    ReDim sNum(ListSize)
    Randomize
    For I = 0 To ListSize - 1
        sNum(I) = Int(Rnd * MaxNum) + 1
    Next
    Text1.Text = Join(sNum, vbNewLine)
    End Sub
    
    Private Sub Form_Load()
    Command1.Value = True
    End Sub
    Note that Join() with the loop starting at 1 leaves a blank line as the first line of the textbox because it adds a null string. I like the string array approach prior to filling the text box. It will likely run faster with longer lists because the text box is not constantly being revised inside the loop. In the old days that would flicker. The array approach might take some more memory, but that's rather cheap.
    Doctor Ed

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width