Creating Controls dynamically in visual basic.net

F

fplisa

Hi-
I have code that dynamically adds 1 to 10 textboxes to a form. The
user is asked how many boxes he/she needs.

Problem is, I can't get to the events of each box that is added EXCEPT
for the last one.

I need to do a running total of the text boxes as the user enters a
value and moves onto the next textbox

I've tryed everything and cn't seem to get it.
I've tried Addhandler.

MY CODE
******************************

Private WithEvents TextBox1 As New TextBox

*****************************************
Public Sub FSA_SetControls(ByVal num As Int32, ByVal nName As
String, ByVal thelocation As Int32, ByVal TypeofControl As String)

TextBox1 = New TextBox
Me.SuspendLayout()

TextBox1.CreateControl()
TextBox1.Location = New Point(296, thelocation) 'left/top
TextBox1.Size = New Size(71, 23) ' width/height
TextBox1.Font = New System.Drawing.Font("Microsoft Sans
Serif", 12)
TextBox1.Font = New System.Drawing.Font(TextBox1.Font,
FontStyle.Bold)
TextBox1.Name = nName '"TextBox1"
TextBox1.Text = nName 'TextBox1.Name
Controls.Add(TextBox1)
Me.ResumeLayout(True)
AddEvents(TextBox1)
End Sub

*****************************************

Private Sub ProcessLeave(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.Leave
MsgBox("TESTING")
End Sub
*****************************************
Private Sub AddEvents(ByVal ctrlParent As Control)
Dim ctrl As Control
For Each ctrl In ctrlParent.Controls
If TypeOf ctrl Is TextBox Then
AddHandler ctrl.Leave, AddressOf ProcessLeave
End If

If ctrl.HasChildren Then
AddEvents(ctrl)
End If
Next
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top