Control.Tag 属性

定义

获取或设置包含有关控件的数据的对象。Gets or sets the object that contains data about the control.

public:
 property System::Object ^ Tag { System::Object ^ get(); void set(System::Object ^ value); };
[System.ComponentModel.Bindable(true)]
[System.ComponentModel.TypeConverter(typeof(System.ComponentModel.StringConverter))]
public object Tag { get; set; }
[<System.ComponentModel.Bindable(true)>]
[<System.ComponentModel.TypeConverter(typeof(System.ComponentModel.StringConverter))>]
member this.Tag : obj with get, set
Public Property Tag As Object

属性值

Object

一个 Object,其中包含有关控件的数据。An Object that contains data about the control. 默认值为 nullThe default is null.

属性

示例

下面的代码示例显示了一个窗体,并将 Customer 其存储在其 Tag 属性中。The following code example displays a form and stores a Customer in its Tag property. 此示例要求您定义了一个派生自命名的类 FormCustomerForm 并且您已定义了 CustomerThis example requires that you have defined a class that derives from Form named CustomerForm and that you have defined a Customer.

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();
   }
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();
}
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

注解

从类派生的任何类型 Object 都可以分配给此属性。Any type derived from the Object class can be assigned to this property. 如果 Tag 通过 Windows 窗体设计器来设置属性,则只能分配文本。If the Tag property is set through the Windows Forms designer, only text can be assigned.

属性的常见用途 Tag 是存储与控件密切关联的数据。A common use for the Tag property is to store data that is closely associated with the control. 例如,如果您具有显示有关客户的信息的控件,则可以将 DataSet 包含客户订单历史记录的设置存储在该控件的属性中, Tag 以便可以快速访问数据。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.

适用于

另请参阅