[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
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!
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
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!
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.
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.
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.