Control.Tag 属性

定义

获取或设置包含有关控件的数据的对象。

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))]
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,其中包含有关控件的数据。 默认值为 null

属性

示例

下面的代码示例显示窗体,并将 存储在 CustomerTag 属性中。 此示例要求已定义派生自 Form named CustomerForm 的类,并且已定义 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 类的任何类型都可以分配给此属性。 Tag如果属性是通过Windows 窗体设计器设置的,则只能分配文本。

属性的 Tag 一个常见用途是存储与 控件密切相关的数据。 例如,如果控件显示有关客户的信息,则可以在该控件的 属性中存储 DataSet 包含客户的订单历史记录的 Tag ,以便可以快速访问数据。

适用于

另请参阅