Understanding the TAG property

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Terrance

    Understanding the TAG property

    Can someone please explain to me the purpose of the tag property for
    WindowsForm objects such as a TEXTBOX? What is the benefit of using this
    property if any? Thanks, a bunch.
    --
    TC
  • Cor Ligthert [MVP]

    #2
    Re: Understanding the TAG property

    Terance,

    Think about the fact that you have to loop through some controls and want
    only those you have marked with "a" (it can even be a class object with more
    parameters). Than by instance this Tag is really extremely useful

    I hope this helps,

    Cor


    Comment

    • Terrance

      #3
      Re: Understanding the TAG property

      Cor:

      Can I use a tag on a object and reference that object in a User Control
      (e.g. ucmdAdd Button)? and if so, how do I go about doing this? I'm using
      VB.net.
      --
      TC


      "Cor Ligthert [MVP]" wrote:
      [color=blue]
      > Terance,
      >
      > Think about the fact that you have to loop through some controls and want
      > only those you have marked with "a" (it can even be a class object with more
      > parameters). Than by instance this Tag is really extremely useful
      >
      > I hope this helps,
      >
      > Cor
      >
      >
      >[/color]

      Comment

      • Morten Wennevik

        #4
        Re: Understanding the TAG property

        Hi Terrance,

        The Tag property is provided so you can associate a Control with some kind of information. The information can be anything you like. As Cor said, you could mark Controls and filter the controls in a loop by the information in the Tag property.

        Another example:

        Consider a bunch of Labels with animal names. You want the user to put animals inside animal pens by dragging from the Labels, but not dragging the Labels themselves. The information in the animal pens is more complex than plain text, so you need some kind of Animal object.

        One solution could be to drag the Label text into the pen an use code to determine which Animal to create and put in it. Another way could be to create all Animals in advance and store the references to these objects inside the Tag properties of the appropriate label. Then, instead of dragging the Label text, you drag the Animal object and put it directly inside the pen.


        --
        Happy coding!
        Morten Wennevik [C# MVP]

        Comment

        Working...