TreeNode.Text 属性
获取或设置在树节点标签中显示的文本。
**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)
语法
声明
<LocalizableAttribute(True)> _
Public Property Text As String
用法
Dim instance As TreeNode
Dim value As String
value = instance.Text
instance.Text = value
[LocalizableAttribute(true)]
public string Text { get; set; }
[LocalizableAttribute(true)]
public:
property String^ Text {
String^ get ();
void set (String^ value);
}
/** @property */
public String get_Text ()
/** @property */
public void set_Text (String value)
public function get Text () : String
public function set Text (value : String)
属性值
在树节点标签中显示的文本。
备注
如果父 TreeView 的 LabelEdit 属性设置为 false,用户将无法设置此属性。要显式设置此属性,一种替代方法是使用 TreeNode 构造函数之一创建树节点,使其具有表示 Text 属性的字符串参数。如果显示标签,则标签显示在 TreeNode 图像的旁边。
示例
下面的代码示例创建一个要为其分配子树节点的根树节点。将 ArrayList 中的每个 Customer
对象的一个子树节点以及分配给 Customer
对象的每个 Order
对象的一个子树节点添加到根树节点。将 Customer
对象分配给 Tag 属性,并将表示 Customer
对象的树节点用 Orange 文本显示。本示例要求已定义 Customer
和 Order
对象,Form 上具有 TreeView 控件,还要求有一个名为 customerArray
的 ArrayList,其中包含 Customer
对象。
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
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);
}
}
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 void AddRootNodes()
{
// Add a root node to assign the customer nodes to.
TreeNode rootNode = new TreeNode();
rootNode.set_Text("CustomerList");
// Add a main root treenode.
myTreeView.get_Nodes().Add(rootNode);
// Add a root treenode for each 'Customer' object in the ArrayList.
for (int iCtr1 = 0; iCtr1 < customerArray.get_Count(); iCtr1++) {
Customer myCustomer = (Customer)customerArray.get_Item(iCtr1);
// Add a child treenode for each Order object.
int i = 0;
TreeNode myTreeNodeArray[] = new TreeNode[5];
for (int iCtr2 = 0; iCtr2 < myCustomer.customerOrders.get_Count();
iCtr2++) {
Order myOrder = (Order)myCustomer.customerOrders.get_Item(iCtr2);
myTreeNodeArray.set_Item(i, new TreeNode(myOrder.orderID));
i++;
}
TreeNode customerNode =
new TreeNode(myCustomer.customerName, myTreeNodeArray);
// Display the customer names with and Orange font.
customerNode.set_ForeColor(Color.get_Orange());
// Store the Customer object in the Tag property of the TreeNode.
customerNode.set_Tag(myCustomer);
myTreeView.get_Nodes().get_Item(0).get_Nodes().Add(customerNode);
}
} //AddRootNodes
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0
请参见
参考
TreeNode 类
TreeNode 成员
System.Windows.Forms 命名空间
LabelEdit
BeginEdit
TreeNode.IsEditing 属性