Dim arrTextBox(0) As TextBox ' (Prepare array to receive textBoxes.
' Give this the necesary scope)
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim txtTemp As TextBox ' ( Reserve variable for new textbox
' Local scope only OK)
Dim iCount As Integer (Local scope only)
For iCount = 0 To 9
ReDim Preserve arrTextBox(iCount) '(Expand array to receive
' next textbox
txtTemp = New TextBox ' ( Create instance of new textbox)
txtTemp.Name = "txtBox" & iCount.ToString '(Name textboxes with
'sequential numbering)
txtTemp.Width = 40
txtTemp.Location = New Point(15, iCount * 20)
Me.Controls.Add(txtTemp) '(Add new textbox to form
arrTextBox(iCount) = txtTemp '(Place reference to new textbox
'in appropriate element of array)
'(The next line adds the handle of the new textbox to the appropriate event)
AddHandler txtTemp.TextChanged, AddressOf TextChangedEvent
Next
End Sub
Private Sub TextChangedEvent(ByVal sender As System.Object, ByVal e As System.EventArgs)
MessageBox.Show(CType(sender, TextBox).Name & " Text was changed")
End Sub