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

Hussein Zahran
Hussein Zahran used Ask the Experts™
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

Open in new window

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)
End Sub

Open in new window

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?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Freelance programmer / Consultant
MyTextBox(i).Enabled = False

Change that to
MyTextBox(i).Enabled = True

If the control is not enabled then, I think, you will never get a click event happening because it isn't allowed to receive the focus.



What a silly mistake by me lolz.

Thank you Andy, issue resolved :)
AndyAinscowFreelance programmer / Consultant

Mistakes like that are easily done and very difficult to spot by oneself.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial