Link to home
Start Free TrialLog in
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
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?
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Hussein Zahran
Hussein Zahran

ASKER

Perfect

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.