My problem is after I created 5 rows of text boxes, I want to add random numbers to all of the text boxes in one time when I click the button.
Does anybody know how to solve this problem??

Here are my coding to create text box dynamically:

Private Sub btnAdd5Row_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddFiveRow.Click

        For Me.count = count To 5
   
            Dim myTextBox2 = New TextBox
            With myTextBox2
                .Name = "txtNum" & count
                .Size = New Size(50, 26)
                .Location = New Point(x, y)
                .Font = txtNum1.Font
                .TextAlign = HorizontalAlignment.Center
                .MaxLength = 4
                Controls.Add(myTextBox2)
            End With

    End Sub

Here is the random number coding.

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim RandomClass As New Random
        Dim RandomNumber As Integer
        RandomNumber = RandomClass.Next(0, 9999)
        



    End Sub

hello !
try this ,

'here is a function 
  Dim objRandom As New System.Random( _
  CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))

    Public Function GetRandomNumber( _
      Optional ByVal Low As Integer = 1, _
      Optional ByVal High As Integer = 100) As Integer
        ' Returns a random number,
        ' between the optional Low and High parameters
        Return objRandom.Next(Low, High + 1)
    End Function
'now place this code at the event where you want to assign random numbers.
      Dim intDiceRoll As Integer
        intDiceRoll = GetRandomNumber(1, 6)
        txt1.text=intDiceRoll.ToString
        txt2.text=intDiceRoll.ToString
        txt3.text=intDiceRoll.ToString
        txt4.text=intDiceRoll.ToString
.
.
.
.

hope this code helps you , if your prob is solved then please mark this thread solved .

Best Regards

hello !
try this ,

'here is a function 
  Dim objRandom As New System.Random( _
  CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))

    Public Function GetRandomNumber( _
      Optional ByVal Low As Integer = 1, _
      Optional ByVal High As Integer = 100) As Integer
        ' Returns a random number,
        ' between the optional Low and High parameters
        Return objRandom.Next(Low, High + 1)
    End Function
'now place this code at the event where you want to assign random numbers.
      Dim intDiceRoll As Integer
        intDiceRoll = GetRandomNumber(1, 6)
        txt1.text=intDiceRoll.ToString
        txt2.text=intDiceRoll.ToString
        txt3.text=intDiceRoll.ToString
        txt4.text=intDiceRoll.ToString
.
.
.
.

hope this code helps you , if your prob is solved then please mark this thread solved .

Best Regards

thx.But my problem is I cant do this "txt4.text=intDiceRoll.ToString' because my textbox create in runtime. I dont have txt4 before i run the program.

You can do it this way:

Private r As New Random()
Private tbs As TextBox()

'method for creating textBoxes:
Private Sub CreatingTextBoxes()
	tbs = New TextBox(4) {}
	Dim locX As Integer = 20, locY As Integer = 20
	'setting initial location of 1st textbox (20, 20)
	For i As Integer = 0 To tbs.Length - 1
		tbs(i).Name = "textBox" & i.ToString()
		tbs(i).Size = New Size(100, 23)
		'set by your self!
		tbs(i).Location = New Point(locX, locY)
		locY += 40
		'next textBox will be bellow this one (-40px)
		Me.Controls.Add(tbs(i))
	Next
End Sub

Private Sub button1_Click(sender As Object, e As EventArgs)
	'now we can put the text into each textBox:
	If tbs IsNot Nothing Then
		For i As Integer = 0 To tbs.Length - 1
				' random number from 1 to 100 will be inserted into each of 5 textboxes.
			tbs(i).Text = r.[Next](1, 101).ToString()
		Next
	End If
End Sub

Hope it helps.

Ok try this.....

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For i As Integer = 1 To 5

            Dim myTextBox2 = New TextBox
            With myTextBox2
                .Name = "txtNum" & i
                .Size = New Size(50, 26)
                .Location = New Point(5, 40 * i)
                .TextAlign = HorizontalAlignment.Center
                .MaxLength = 4
                Controls.Add(myTextBox2)
            End With
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim RandomClass As New Random
        Dim RandomNumber As Integer


        For i As Integer = 1 To 5
            Dim t As TextBox = Me.Controls.Item("txtNum" + i.ToString.Trim)
            RandomNumber = RandomClass.Next(0, 9999)
            t.Text = RandomNumber.ToString
        Next

    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.