Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 4
Control Class
Control Properties
 Tag Property
Collapse All/Expand All Collapse All
.NET Framework Class Library
Control..::.Tag Property

Gets or sets the object that contains data about the control.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Visual Basic
<BindableAttribute(True)> _
<TypeConverterAttribute(GetType(StringConverter))> _
Public Property Tag As Object
C#
[BindableAttribute(true)]
[TypeConverterAttribute(typeof(StringConverter))]
public Object Tag { get; set; }
Visual C++
[BindableAttribute(true)]
[TypeConverterAttribute(typeof(StringConverter))]
public:
property Object^ Tag {
    Object^ get ();
    void set (Object^ value);
}
F#
[<BindableAttribute(true)>]
[<TypeConverterAttribute(typeof(StringConverter))>]
member Tag : Object with get, set

Property Value

Type: System..::.Object
An Object that contains data about the control. The default is nullNothingnullptra null reference (Nothing in Visual Basic).

Any type derived from the Object class can be assigned to this property. If the Tag property is set through the Windows Forms designer, only text can be assigned.

A common use for the Tag property is to store data that is closely associated with the control. For example, if you have a control that displays information about a customer, you might store a DataSet that contains the customer's order history in that control's Tag property so the data can be accessed quickly.

The following code example displays a form and stores a Customer in its Tag property. This example requires that you have defined a class that derives from Form named CustomerForm and that you have defined a Customer.

Visual Basic
Private Sub buttonNewCustomer_Click(sender As Object, _
  e As EventArgs) Handles buttonNewCustomer.Click
   ' Create a new customer form and assign a new 
   ' Customer object to the Tag property. 
   Dim customerForm As New CustomerForm()
   customerForm.Tag = New Customer()
   customerForm.Show()
End Sub
C#
private void buttonNewCustomer_Click(object sender, EventArgs e)
{
   /* Create a new customer form and assign a new 
    * Customer object to the Tag property. */
   CustomerForm customerForm = new CustomerForm();
   customerForm.Tag = new Customer();
   customerForm.Show();
}
Visual C++
private:
   void buttonNewCustomer_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      /* Create a new customer form and assign a new
                  * Customer object to the Tag property. */
      CustomerForm^ customerForm = gcnew CustomerForm;
      customerForm->Tag = gcnew Customer;
      customerForm->Show();
   }

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker