This post originated from an RSS feed registered with .NET Buzz
by Pau Laudeman.
Original Post: Windows Forms Quick Tip: Use your Tag property!
Feed Title: Paul Laudeman
Feed URL: /error.htm?aspxerrorpath=/blogs/paul.laudeman/rss.aspx
Feed Description: Helping You to Make "Smart Clients" Smarter!
A question I frequently come across is how to add some type of context information to UI controls in a Windows Forms project. For instance, you might have a textbox where you want to revert to the original value if a user decides to cancel edits, you could store the original value in the Tag property and pull that out when needed (if you're not using data binding). Another example might be if you have a TreeView control and need to add information about the nodes to help perform some action when a user clicks on a node.
In either case, and many more, you should consider using the Tag property of the Control object. As you know, all Windows Forms controls (and even Forms!) inherit from the Control class which offers the Tag property. Since the Tag property is of type Object, this means that you can use it to store anything you want! You could use it to store a string, an array, a full blown class, etc., depending on your needs.