MSDN里的一個解釋:
一個 Object,它包含有關控件的數據。
Tag 屬性的一個常見用途,是存儲與控件密切關聯的數據。例如,如果有一個顯示客戶信息的控件,則可以將包含客戶訂購歷史的 DataSet 存儲在該控件的 Tag 屬性中,以便可以快速訪問數據。
你確實可以另外設一個變量做標記,用來存儲如bool,string,int 等等。
但你也可以把一些控件的相關數據存放在TAG中,之所以使用TAG,我認為是出於可以快速的訪問與此控件相關的數據,或者說是因為你覺得臨時的定義一個變量來存儲那個控件的數據的話,有點麻煩,還不如將這個數據直接暫時
儲存在它的TAG屬性中算了,免得去編寫定義變量的代碼.反正幾乎每個控件都有TAG屬性,你可以將相關控件的相關數據 存放在TAG中。
----------------------------------------------------------------------------------------------------------------------
Control.Tag 屬性
獲取或設置包含有關控件的數據的對象。
屬性值
類型:System.Object
一個 Object,它包含有關控件的數據。默認為 null。
下面的代碼示例顯示一個窗體並將 Customer 存儲在其 Tag 屬性中。該示例要求已經定義了一個從 Form 派生的、名為 CustomerForm 的類,並且已經定義了一個 Customer。
C#
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();
}
-------------------------------------------------------------------------------------------------
http://www.dotnetperls.com/tag
The Tag property stores an object reference. Windows Forms programs can use object models of arbitrary complexity. But the Tag property is a simple way to link a certain object to a certain control. It is useful in certain situations.
Property
Example
To start, this program shows the use of the Tag property inside the object model of the Windows Forms program. The Form1 constructor, which instantiates the control, assigns the Tag property to a new ExampleTag—a custom type.
Then:This object reference can be accessed at any time and in any method later in the event loop of the Windows program.
Program that uses Tag property: C# using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); // Set tag on constructor. this.Tag = new ExampleTag(1, 2); } private void Form1_Load(object sender, EventArgs e) { // When form loads, render tag to the title bar. this.Text = this.Tag.ToString(); } } class ExampleTag { public int _a; public int _b; public ExampleTag(int a, int b) { // Store the fields. this._a = a; this._b = b; } public override string ToString() { // Write the fields to a string. return string.Format("Tag a = {0}, b = {1}", this._a, this._b); } } } Output The window is displayed with the title bar reading the tag ToString output.
In the Form1 constructor, the ExampleTag constructor is called. It returns an ExampleTag object reference, which is copied to the Tag reference field. The ExampleTag remains in memory. It is pointed to by the Tag property's backing store.
Next:In the Form1_Load event handler, we see that you can access the Tag property on the enclosing control.
And:This is the same Tag that we previously set. We could also modify the ExampleTag object at this point.
The Form1_Load event accesses the Tag reference as the base type object. However, when you invoke the ToString method upon a base class, the most derived method is actually called, typically through a matrix data structure.
ToString Method
Thus:You can see that the title bar of the program is equal to the result of the ToString method.
When planning a program, the object model is an important part of the program's architecture. If you use the Tag property too much, you will conflate the user interface specific information in the program with the data model.
Caution:This makes portability of your program to new interfaces more difficult.
Summary
We explored an example usage of the Tag property in the Windows Forms widget layout system. The Tag property essentially provides a user-defined field that can store any form of object in a persistent way.
And:It provides a convenient hook to your data. But it conflates user interface concepts and more abstract object models.