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
Next

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)
   MsgBox(TxtBox.Text)
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?
Hussein ZahranAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Hussein ZahranAuthor Commented:
Perfect

What a silly mistake by me lolz.

Thank you Andy, issue resolved :)
AndyAinscowFreelance programmer / ConsultantCommented:
Mistakes like that are easily done and very difficult to spot by oneself.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.