troubleshooting Question

Issue with Binding an Event Handler to a Dynamically-Created Control at Runtime

Avatar of Hussein Zahran
Hussein Zahran asked on
Visual Basic.NET.NET Programming
3 Comments1 Solution88 ViewsLast Modified:
Hello Dear Experts

In my Windows-Form application, and upon its main form load event, I query my database to get a dataset of records.

The number of those records changes from time to another. Sometimes myDataset.Tables(0).Rows.Count = 3 and some other times it's 4 and another time it's 10, so it depends.

I want to create a TextBox control for each single record of them at run time, as well as I want to bind a "Click" Event Handler Event for each of those runtime-created TextBox controls. So this is the code I use to do so:

Dim MyTextBox(myDataset.Tables(0).Rows.Count ) As TextBox

For i = 0 To myDataset.Tables(0).Rows.Count - 1
   MyTextBox(i) = New TextBox
   MyTextBox(i).Parent = Me
   MyTextBox(i).ReadOnly = True
   MyTextBox(i).Multiline = True
   MyTextBox(i).Enabled = False
   MyTextBox(i).Size = New System.Drawing.Size(... Some Size Here ...)
   MyTextBox(i).BorderStyle = BorderStyle.FixedSingle
   MyTextBox(i).TabIndex = 0
   MyTextBox(i).TextAlign = HorizontalAlignment.Left
   MyTextBox(i).Name = "MyTextBox" + i.ToString
   MyTextBox(i).Text = "MyTextBox_Text_" + i.ToString
   MyTextBox(i).Font = New System.Drawing.Font("Tahoma", 8.0!, System.Drawing.FontStyle.Bold)
   MyTextBox(i).Location = New Point(... Some Location Here ...)
   AddHandler MyTextBox(i).Click, AddressOf MyTextBox_Click
Next

This will create for me a total of (myDataset.Tables(0).Rows.Count) TextBox controls in my main form at run time, having each one of those controls being bound to a "Click" event handler that is taken care of by the "MyTextBox_Click" method which has the following definition:

Private Sub MyTextBox_Click(ByVal sender As Object, ByVal e As EventArgs)
   Dim TxtBox As TextBox = DirectCast(sender, TextBox)
   MsgBox(TxtBox.Text)
End Sub

This is what I know about how to enable an Event Handler at run time for a runtime-created control in my form.

Now, when I run my application and see those runtime created TextBox controls, then I click on any of them, I'm supposed to have a MsgBox popup for me with some text in it, but the issue is: nothing happens, no MsgBox popup at all.

I even run my application in Debug Mode and discovered that the applicatoin does not even call this "MyTextBox_Click" function when I click on any of the runtime-created TextBox control.

Anyone has any idea why? Am I missing something here?
ASKER CERTIFIED SOLUTION
AndyAinscow
Freelance programmer / Consultant

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros