7 Replies - 21785 Views - Last Post: 01 November 2011 - 12:43 PM Rate Topic: -----

#1 odiepus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 01-November 11

Can someone please explain the .Tag property

Posted 01 November 2011 - 11:44 AM

Using Murach 2008 C# and it briefly goes over what .Tag is. All I understand is that, if you set the Tag property on for example a textbox, then another form will be able to access it and process it somehow. But I just can't see how its done. I have Googled it and there is nothing that explains it in a way that I am able to understand. I also searched this site. I'm starting to think that it may be so simple that there is no need for further explanation, but I want to make sure. I'm a computer science major and I feel that I need to understand as much as possible.

Assume I have another form that has a textbox named txtPrice. The Tag property is set to Price. The code below is a different class that validates whether input is valid for the textbox.

Now this what I am working with, but please feel free to use another example. I only want to understand the concept. I am sure that once I know the concept, then I'll be able to see it working in other examples.

public static bool IsDecimal(TextBox textBox)
		{
			try
			{
				Convert.ToDecimal(textBox.Text);
				return true;
			}
			catch (FormatException)
			{
				MessageBox.Show([color="#2E8B57"]textBox.Tag[/color] + " must be a decimal number.", Title);
				textBox.Focus();
				return false;
			}
		}



Is This A Good Question/Topic? 0
  • +

Replies To: Can someone please explain the .Tag property

#2 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: Can someone please explain the .Tag property

Posted 01 November 2011 - 11:48 AM

The .Tag property is just an object that allows you to store some arbitrary information about a particular Control. It's not really special in that you can use it in other classes or forms, it's exactly the same as other properties in a TextBox like .Text
Was This Post Helpful? 3
  • +
  • -

#3 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Can someone please explain the .Tag property

Posted 01 November 2011 - 11:56 AM

I agree with Ryano121. It's nothing special. Here is an extremely stupid example of using the Tag property.

Say you have a TextBox that you want to show the date as a string. But let's say that later on, you want that exact date as a DateTime. Instead of having to convert the string back to the date, you can simply set the original DateTime object to the Tag property.

DateTime now = DateTime.Now;
txtDate.Text = now.ToString("yy/MM/dd");
txtDate.Tag = now;

// then later on
DateTime originalDate = txtDate.Tag as DateTime;
// now do something with the original date



You should NOT access form controls from a separate form like you show above. That is not the purpose of the Tag property.
Was This Post Helpful? 1
  • +
  • -

#4 odiepus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 01-November 11

Re: Can someone please explain the .Tag property

Posted 01 November 2011 - 12:16 PM

In your example, two objects were created. One that is the string form of DateTime that was converted. And the other that has the Tag property set to "now". So now, we have two different values for the same textbox. Is this correct?

How I see it;

textbox
____ref______| |___ref_______
| |
.Tag = now; .Text = "yy/MM/dd"

Meant for diagram to look like this;

textbox ---ref-----> .Text = "yy/MM/dd";

same textbox as above ----ref----> .Tag = now;
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Can someone please explain the .Tag property

Posted 01 November 2011 - 12:22 PM

View Postodiepus, on 01 November 2011 - 03:16 PM, said:

In your example, two objects were created. One that is the string form of DateTime that was converted. And the other that has the Tag property set to "now".


Not really. The Textbox already has those properties created, so I am not "creating" two more objects. I am simply assigning values to those properties(which already have values).

Quote

So now, we have two different values for the same textbox. Is this correct?


The Textbox has a number of properties. I wouldn't consider these "values". If somebody were to ask me the "value" of a TextBox, I would tell them what the Text property was. So to answer your question, I would say no, it's not "two different values for the same textbox".
Was This Post Helpful? 1
  • +
  • -

#6 Martyr2   User is online

  • Programming Theoretician
  • member icon

Reputation: 5552
  • View blog
  • Posts: 14,558
  • Joined: 18-April 07

Re: Can someone please explain the .Tag property

Posted 01 November 2011 - 12:29 PM

In other words, think of the tag property as a pouch that hangs on the belt of an object. This pouch can only hold 1 object. In the examples above, eclipse created a dateTime object, and stored it in the pouch of txtDate textbox. In the second half of the example, he pulls out the object from the tag property and casts it back to a DateTime object to use.

That is all the tag property is for, to store an object along with the control. I believe its history actually goes back to a time where it use to only handle a string and was used to handle a small tidbit of text data like a second name for the control (this is how it still works when you set the property through the designer).

:)
Was This Post Helpful? 2
  • +
  • -

#7 odiepus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 01-November 11

Re: Can someone please explain the .Tag property

Posted 01 November 2011 - 12:36 PM

Got it!! Thanks all for the help. I really appreciate it. Information was clear and therefore made a whole lot of sense.
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Can someone please explain the .Tag property

Posted 01 November 2011 - 12:43 PM

It hold a reference to a single object, that object could hold many objects.

.Tag= New List(Of MySuperDuperExcitingObject)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1