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

屬性值

包含有關控制項資料的 ObjectAn 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 Forms 設計工具來設定,則只能指派文字。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. 例如,如果您有顯示客戶相關資訊的控制項,您可能會在該控制項的 Tag 屬性中儲存包含客戶訂單歷程記錄的 DataSet,讓資料可以快速存取。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.

適用於

另請參閱