How to use the Control.Tag?

  • Thread starter Thread starter Jason Huang
  • Start date Start date
J

Jason Huang

Hi,

I am wondering how to use the .Tag in a Windows.Control, such as
MyTextBox.Tag.
Would someone give me some advice?
Thanks for help.


Jason
 
Hi,

I am wondering how to use the .Tag in a Windows.Control, such as
MyTextBox.Tag.
Would someone give me some advice?
Thanks for help.

What do you want to use it for? At runtime you can store anything in there
from additional text to a full dataset. Typically I think you'll see it
used to store additional data about what is being displayed in the control.
For example maybe you are showing a person's name in the control, but you
have an complete Person class that you loaded to get that name that
includes additional data such as date of birth, address, etc. You could
store the Person class in the control's Tag property for quick access.

Another usage might be to store a code value and display in the control
descriptive text. An example would be the state portion of a US address.
On the screen you would display Georgia, but you are probably only storing
GA in your backend data store since that is what the US postal service is
going to want. So you set the text to Georgia but hang on to the code of
GA by placing it in the Tag.

These are just two examples. The point is that you can store anything you
want in there.
 
how to use the .Tag in a Windows.Control
My recommendation is to not use this property at all. It's a kludgy
carryover from the older-generation dev languages. (VB?) Personally, if I
want to store some additional information in a control, I'll just extend the
control. So, instead of putting my Employee in the Tag property of a
Textbox, I'll just create an Employee by extending Textbox. It's easy and
you get a strongly-typed Employee property instead of having to cast the Tag
(Object) to Employee every time you want to get to the Employee-specific
properties.

- Mitchell S. Honnert
 
Back
Top