How can I determine which control I am using (vb.net cf)

I have a problem with dynamic controls.

I am creating a group of controls for each post to display on a form.

I add the post ID as a tag for each control to determine which post it belongs 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 store the value of a tag How can I determine which control has called it

0


a source to share


1 answer


Let's say your SetContactNumber event is defined as:

FROM#

  private void SetContactNumber(object sender, EventArgs e)
  {
      //Stuff that happens when the SetContactNumber event is raised...
  }

      

VB

  Private sub SetContactNumber(sender As object, e As EventArgs)
      //Stuff that happens when the SetContactNumber event is raised
  End Sub

      

The Sender parameter is the object that raised the event. So you just need to drop it and attach the value to the tag:

FROM#

  ((textbox)sender).tag = "Whatever you wanted to put in here";

      



VB

  CType(sender, textbox).tag = "Whatever you wanted to put in here"

      

A tag property takes the value of an object of the type, so the assigned value can be anything you like: string, object, class instance, etc. It is your responsibility to discard this object when you pull it from the tag property to use it though.

So when you put it all together, this will pull in the object that raised the event, dropped it as a textbox, and unloaded the value you specified in the tag property.

FROM#

  private void SetContactNumber(object sender, EventArgs e)
  {
      textbox thisTextbox = (textbox)sender;
      thisTextbox.tag = "Whatever you wanted to put in here";
  }

      

VB

  Private Sub SetContactNumber(sender As Object, e As EventArgs)
      Dim thisTextbox As TextBox = CType(sender, Textbox)
      thisTextbox.tag = "Whatever you wanted to put in here"
  End Sub

      

+4


a source







All Articles