Avatar of Hussein Zahran
Hussein Zahran
 asked on

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

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?
Visual Basic.NET.NET Programming

Avatar of undefined
Last Comment

8/22/2022 - Mon

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Hussein Zahran


What a silly mistake by me lolz.

Thank you Andy, issue resolved :)

Mistakes like that are easily done and very difficult to spot by oneself.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy