TreeNode.Tag 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定包含有關樹狀節點資料的物件。
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
。
- 屬性
範例
下列程式碼範例會建立要指派子樹狀節點的根樹節點。 中 ArrayList 每個 Customer
物件的子樹節點會加入根樹狀節點,以及指派給 Customer
物件之每個 Order
物件的子樹狀節點。 物件 Customer
會指派給 Tag 屬性,而表示 Customer
物件的樹狀節點會以 Orange 文字顯示。 這個範例會要求您已 Customer
定義 和 Order
物件、 TreeView 上的 Form 控制項,以及 ArrayList 包含 Customer
物件的具名 customerArray
。
ref class Customer
{
public:
ArrayList^ CustomerOrders;
String^ CustomerName;
Customer( String^ myName )
{
CustomerName = myName;
CustomerOrders = gcnew ArrayList;
}
};
ref class Order
{
public:
String^ OrderID;
Order( String^ myOrderID )
{
this->OrderID = myOrderID;
}
};
void AddRootNodes()
{
// Add a root node to assign the customer nodes to.
TreeNode^ rootNode = gcnew TreeNode;
rootNode->Text = "CustomerList";
// Add a main root treenode.
myTreeView->Nodes->Add( rootNode );
// Add a root treenode for each 'Customer' object in the ArrayList.
IEnumerator^ myEnum = customerArray->GetEnumerator();
while ( myEnum->MoveNext() )
{
Customer^ myCustomer = safe_cast<Customer^>(myEnum->Current);
// Add a child treenode for each Order object.
int i = 0;
array<TreeNode^>^myTreeNodeArray = gcnew array<TreeNode^>(5);
IEnumerator^ myEnum = myCustomer->CustomerOrders->GetEnumerator();
while ( myEnum->MoveNext() )
{
Order^ myOrder = safe_cast<Order^>(myEnum->Current);
myTreeNodeArray[ i ] = gcnew TreeNode( myOrder->OrderID );
i++;
}
TreeNode^ customerNode = gcnew TreeNode( myCustomer->CustomerName,myTreeNodeArray );
// Display the customer names with and Orange font.
customerNode->ForeColor = Color::Orange;
// Store the Customer Object* in the Tag property of the TreeNode.
customerNode->Tag = myCustomer;
myTreeView->Nodes[ 0 ]->Nodes->Add( customerNode );
}
}
public class Customer
{
public ArrayList CustomerOrders;
public string CustomerName;
public Customer(string myName)
{
CustomerName = myName;
CustomerOrders = new ArrayList();
}
}
public class Order
{
public string OrderID;
public Order(string myOrderID )
{
this.OrderID = myOrderID;
}
}
public void AddRootNodes()
{
// Add a root node to assign the customer nodes to.
TreeNode rootNode = new TreeNode();
rootNode.Text = "CustomerList";
// Add a main root treenode.
myTreeView.Nodes.Add(rootNode);
// Add a root treenode for each 'Customer' object in the ArrayList.
foreach(Customer myCustomer in customerArray)
{
// Add a child treenode for each Order object.
int i = 0;
TreeNode[] myTreeNodeArray = new TreeNode[5];
foreach(Order myOrder in myCustomer.CustomerOrders)
{
myTreeNodeArray[i] = new TreeNode(myOrder.OrderID);
i++;
}
TreeNode customerNode = new TreeNode(myCustomer.CustomerName,
myTreeNodeArray);
// Display the customer names with and Orange font.
customerNode.ForeColor = Color.Orange;
// Store the Customer object in the Tag property of the TreeNode.
customerNode.Tag = myCustomer;
myTreeView.Nodes[0].Nodes.Add(customerNode);
}
}
Public Class Customer
Public CustomerOrders As ArrayList
Public CustomerName As String
Public Sub New(myName As String)
CustomerName = myName
CustomerOrders = New ArrayList()
End Sub
End Class
Public Class Order
Public OrderID As String
Public Sub New(myOrderID As String)
Me.OrderID = myOrderID
End Sub
End Class
Public Sub AddRootNodes()
' Add a root node to assign the customer nodes to.
Dim rootNode As TreeNode
rootNode = New TreeNode()
rootNode.Text = "CustomerList"
' Add a main root treenode.
myTreeView.Nodes.Add(rootNode)
' Add a root treenode for each Customer object in the ArrayList.
Dim myCustomer As Customer
For Each myCustomer In customerArray
' Add a child treenode for each Order object.
Dim i As Integer = 0
Dim myTreeNodeArray(4) As TreeNode
Dim myOrder As Order
For Each myOrder In myCustomer.CustomerOrders
myTreeNodeArray(i) = New TreeNode(myOrder.OrderID)
i += 1
Next myOrder
Dim customerNode As New TreeNode(myCustomer.CustomerName, _
myTreeNodeArray)
' Display the customer names with and Orange font.
customerNode.ForeColor = Color.Orange
' Store the Customer object in the Tag property of the TreeNode.
customerNode.Tag = myCustomer
myTreeView.Nodes(0).Nodes.Add(customerNode)
Next myCustomer
End Sub
備註
任何 Object 衍生類型都可以指派給這個屬性。 如果透過Windows Forms設計工具設定此屬性,則只能指派文字。