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; }
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.

属性

示例

下面的代码示例显示一个窗体,并在其 Tag 属性中存储 CustomerThe following code example displays a form and stores a Customer in its Tag property. 此示例要求您定义了一个派生自名为 CustomerForm Form 的类,并且您已定义了一个 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. 如果通过 Windows 窗体设计器设置 Tag 属性,则只能分配文本。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.

适用于

另请参阅