Control.Tag Control.Tag Control.Tag Control.Tag Property

定义

获取或设置包含有关控件的数据的对象。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; }
member this.Tag : obj with get, set
Public Property Tag As 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. 此示例需要您定义了派生的类Form名为CustomerForm和已定义的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.

适用于

另请参阅