Store Arbitrary Data in a Control

Problem

You want to link a piece of data to a control.

Solution

Use the Control.Tag property.

Discussion

Every class that derives from System.Windows.Forms.Control provides a Tag property that can be used to store an instance of any type of object. The Tag property isn’t used by the control or the .NET Framework. Instead, it’s reserved as a convenient storage place for application-specific information. When retrieving data from the Tag property, you’ll need to use the CType function to cast the object from the generic System.Object type to its original type.

Many other classes that are used with .NET controls also provide a Tag property. Notable examples include the ListViewItem and TreeNode classes (which represent ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.