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))>]
member this.Tag : obj with get, set
Public Property Tag As Object
属性值
Object,包含有关树节点的数据。 默认值为 null
。
- 属性
示例
下面的代码示例创建一个根树节点,以向其分配子树节点。 每个对象的ArrayList子树节点Customer
将添加到根树节点,以及分配给Customer
该对象的每个Order
对象的子树节点。 对象 Customer
分配给 Tag 该属性,表示对象的树节点 Customer
以 Orange 文本显示。 此示例要求你已定义一个Customer
和Order
对象、一个FormTreeView控件以及一个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 窗体设计器设置此属性,则只能分配文本。