I have an issue with dynamic, runtime controls.
I create a group of controls per record to display on a form.
I add the record ID as a tag on each of the controls to identify which record they belong to.
While rCONT.Read
Dim txtphome As New TextBox
txtphome.Name = "phone" + rCONT.Item("pcontID").ToString
txtphome.Text = rCONT.Item("pcontPhHome").ToString
txtphome.Tag = rCONT.Item("pcontID").ToString
tcPatientDetails.TabPages(2).Controls.Add(txtphome)
AddHandler txtphome.LostFocus, AddressOf SaveContactChange
AddHandler txtphome.GotFocus, AddressOf SetContactNumber
End While
In SetContactNumber I want to save the tag value How can I identify which control triggered it
Let’s say your SetContactNumber event is defined as:
C#
VB
The Sender parameter is the object that raised the event. So you just need to cast it and attach the value to the tag:
C#
VB
The tag property takes a value of type object, so the value assigned could be anything you like: string, object, instance of a class etc. It’s your responsibility to cast that object when you’re pulling it out of the tag property to use it though.
So when you put it all together, this will pull the object that raised the event, cast it as a textbox and dump the value you specify into the tag property.
C#
VB