public ref class TreeView : System::Windows::Forms::Control
public class TreeView : System.Windows.Forms.Control
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Windows.Forms.Docking(System.Windows.Forms.DockingBehavior.Ask)]
public class TreeView : System.Windows.Forms.Control
[System.Windows.Forms.Docking(System.Windows.Forms.DockingBehavior.Ask)]
public class TreeView : System.Windows.Forms.Control
type TreeView = class
inherit Control
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Windows.Forms.Docking(System.Windows.Forms.DockingBehavior.Ask)>]
type TreeView = class
inherit Control
[<System.Windows.Forms.Docking(System.Windows.Forms.DockingBehavior.Ask)>]
type TreeView = class
inherit Control
Public Class TreeView
Inherits Control
TreeView
treeView1.BeginUpdate();
treeView1.Nodes.Add("Parent");
treeView1.Nodes[0].Nodes.Add("Child 1");
treeView1.Nodes[0].Nodes.Add("Child 2");
treeView1.Nodes[0].Nodes[1].Nodes.Add("Grandchild");
treeView1.Nodes[0].Nodes[1].Nodes[0].Nodes.Add("Great Grandchild");
treeView1.EndUpdate();
' Populates a TreeView control with example nodes.
Private Sub InitializeTreeView()
treeView1.BeginUpdate()
treeView1.Nodes.Add("Parent")
treeView1.Nodes(0).Nodes.Add("Child 1")
treeView1.Nodes(0).Nodes.Add("Child 2")
treeView1.Nodes(0).Nodes(1).Nodes.Add("Grandchild")
treeView1.Nodes(0).Nodes(1).Nodes(0).Nodes.Add("Great Grandchild")
treeView1.EndUpdate()
End Sub
以下更复杂的代码示例在控件中
TreeView
显示客户信息。 根树节点显示客户名称,子树节点显示分配给每个客户的订单号。 在此示例中,将显示 1,000 个客户,每个订单 15 个。 通过使用和方法取消重新绘制,
TreeView
创建和绘制
TreeNode
对象时
TreeView
会显示等待
Cursor
。
EndUpdate
BeginUpdate
此示例要求你有一个
Customer
可以保存对象集合
Order
的对象。 它还要求你有一个在应用程序目录中命名
MyWait.cur
的游标文件,并且已在应用程序
Form
目录中创建了控件的
TreeView
实例。
// The basic Customer class.
ref class Customer: public System::Object
private:
String^ custName;
protected:
ArrayList^ custOrders;
public:
Customer( String^ customername )
custName = "";
custOrders = gcnew ArrayList;
this->custName = customername;
property String^ CustomerName
String^ get()
return this->custName;
void set( String^ value )
this->custName = value;
property ArrayList^ CustomerOrders
ArrayList^ get()
return this->custOrders;
// End Customer class
// The basic customer Order class.
ref class Order: public System::Object
private:
String^ ordID;
public:
Order( String^ orderid )
ordID = "";
this->ordID = orderid;
property String^ OrderID
String^ get()
return this->ordID;
void set( String^ value )
this->ordID = value;
// End Order class
void FillMyTreeView()
// Add customers to the ArrayList of Customer objects.
for ( int x = 0; x < 1000; x++ )
customerArray->Add( gcnew Customer( "Customer " + x ) );
// Add orders to each Customer object in the ArrayList.
IEnumerator^ myEnum = customerArray->GetEnumerator();
while ( myEnum->MoveNext() )
Customer^ customer1 = safe_cast<Customer^>(myEnum->Current);
for ( int y = 0; y < 15; y++ )
customer1->CustomerOrders->Add( gcnew Order( "Order " + y ) );
// Display a wait cursor while the TreeNodes are being created.
::Cursor::Current = gcnew System::Windows::Forms::Cursor( "MyWait.cur" );
// Suppress repainting the TreeView until all the objects have been created.
treeView1->BeginUpdate();
// Clear the TreeView each time the method is called.
treeView1->Nodes->Clear();
// Add a root TreeNode for each Customer object in the ArrayList.
myEnum = customerArray->GetEnumerator();
while ( myEnum->MoveNext() )
Customer^ customer2 = safe_cast<Customer^>(myEnum->Current);
treeView1->Nodes->Add( gcnew TreeNode( customer2->CustomerName ) );
// Add a child treenode for each Order object in the current Customer object.
IEnumerator^ myEnum = customer2->CustomerOrders->GetEnumerator();
while ( myEnum->MoveNext() )
Order^ order1 = safe_cast<Order^>(myEnum->Current);
treeView1->Nodes[ customerArray->IndexOf( customer2 ) ]->Nodes->Add( gcnew TreeNode( customer2->CustomerName + "." + order1->OrderID ) );
// Reset the cursor to the default for all controls.
::Cursor::Current = Cursors::Default;
// Begin repainting the TreeView.
treeView1->EndUpdate();
// The basic Customer class.
public class Customer : System.Object
private string custName = "";
protected ArrayList custOrders = new ArrayList();
public Customer(string customername)
this.custName = customername;
public string CustomerName
get{return this.custName;}
set{this.custName = value;}
public ArrayList CustomerOrders
get{return this.custOrders;}
} // End Customer class
// The basic customer Order class.
public class Order : System.Object
private string ordID = "";
public Order(string orderid)
this.ordID = orderid;
public string OrderID
get{return this.ordID;}
set{this.ordID = value;}
} // End Order class
// Create a new ArrayList to hold the Customer objects.
private ArrayList customerArray = new ArrayList();
private void FillMyTreeView()
// Add customers to the ArrayList of Customer objects.
for(int x=0; x<1000; x++)
customerArray.Add(new Customer("Customer" + x.ToString()));
// Add orders to each Customer object in the ArrayList.
foreach(Customer customer1 in customerArray)
for(int y=0; y<15; y++)
customer1.CustomerOrders.Add(new Order("Order" + y.ToString()));
// Display a wait cursor while the TreeNodes are being created.
Cursor.Current = new Cursor("MyWait.cur");
// Suppress repainting the TreeView until all the objects have been created.
treeView1.BeginUpdate();
// Clear the TreeView each time the method is called.
treeView1.Nodes.Clear();
// Add a root TreeNode for each Customer object in the ArrayList.
foreach(Customer customer2 in customerArray)
treeView1.Nodes.Add(new TreeNode(customer2.CustomerName));
// Add a child treenode for each Order object in the current Customer object.
foreach(Order order1 in customer2.CustomerOrders)
treeView1.Nodes[customerArray.IndexOf(customer2)].Nodes.Add(
new TreeNode(customer2.CustomerName + "." + order1.OrderID));
// Reset the cursor to the default for all controls.
Cursor.Current = Cursors.Default;
// Begin repainting the TreeView.
treeView1.EndUpdate();
Public Class Customer
Inherits [Object]
Private custName As String = ""
Friend custOrders As New ArrayList()
Public Sub New(ByVal customername As String)
Me.custName = customername
End Sub
Public Property CustomerName() As String
Return Me.custName
End Get
Set(ByVal Value As String)
Me.custName = Value
End Set
End Property
Public ReadOnly Property CustomerOrders() As ArrayList
Return Me.custOrders
End Get
End Property
End Class
Public Class Order
Inherits [Object]
Private ordID As String
Public Sub New(ByVal orderid As String)
Me.ordID = orderid
End Sub
Public Property OrderID() As String
Return Me.ordID
End Get
Set(ByVal Value As String)
Me.ordID = Value
End Set
End Property
End Class
' Create a new ArrayList to hold the Customer objects.
Private customerArray As New ArrayList()
Private Sub FillMyTreeView()
' Add customers to the ArrayList of Customer objects.
Dim x As Integer
For x = 0 To 999
customerArray.Add(New Customer("Customer" + x.ToString()))
Next x
' Add orders to each Customer object in the ArrayList.
Dim customer1 As Customer
For Each customer1 In customerArray
Dim y As Integer
For y = 0 To 14
customer1.CustomerOrders.Add(New Order("Order" + y.ToString()))
Next y
Next customer1
' Display a wait cursor while the TreeNodes are being created.
Cursor.Current = New Cursor("MyWait.cur")
' Suppress repainting the TreeView until all the objects have been created.
treeView1.BeginUpdate()
' Clear the TreeView each time the method is called.
treeView1.Nodes.Clear()
' Add a root TreeNode for each Customer object in the ArrayList.
Dim customer2 As Customer
For Each customer2 In customerArray
treeView1.Nodes.Add(New TreeNode(customer2.CustomerName))
' Add a child TreeNode for each Order object in the current Customer object.
Dim order1 As Order
For Each order1 In customer2.CustomerOrders
treeView1.Nodes(customerArray.IndexOf(customer2)).Nodes.Add( _
New TreeNode(customer2.CustomerName + "." + order1.OrderID))
Next order1
Next customer2
' Reset the cursor to the default for all controls.
Cursor.Current = System.Windows.Forms.Cursors.Default
' Begin repainting the TreeView.
treeView1.EndUpdate()
End Sub
集合
Nodes
包含分配给
TreeView
控件的所有
TreeNode
对象。 此集合中的树节点称为根树节点。 随后添加到根树节点的任何树节点称为子节点。 由于每个
TreeNode
对象可以包含其他
TreeNode
对象的集合,因此在循环访问集合时,你可能会发现很难确定树结构中的位置。 可以使用字符串值来分析
TreeNode.FullPath
字符串
PathSeparator
,以确定标签的开始和结束位置
TreeNode
。
可以通过向属性分配
ImageList
ImageList
并引用要分配
Image
的
Image
ImageList
索引值来显示树节点旁边的图像。 使用以下属性分配图像:
将
ImageIndex
属性设置为未选择树节点时要显示的索引值
Image
。
将
SelectedImageIndex
属性设置为在选定树节点时要显示的索引值
Image
。
所
ImageIndex
引用的图像和
SelectedImageIndex
属性值是分配给
Nodes
集合的所有树节点显示的默认图像。 单个树节点可以通过设置
TreeNode.ImageIndex
和
TreeNode.SelectedImageIndex
属性来替代默认图像。
默认情况下,
TreeView
中显示的状态图像为 16 x 16 像素。
ImageSize
设置属性
StateImageList
不会影响图像的显示方式。 但是,当app.config文件包含以下条目时,状态图像会根据系统 DPI 设置调整大小:
<appSettings>
<add key="EnableWindowsFormsHighDpiAutoResizing" value="true" />
</appSettings>
可以展开树节点以显示下一级别的子树节点。 用户可以通过单击加号 (+) 按钮来展开TreeNode该按钮(如果旁边显示TreeNode一个,也可以通过调用TreeNode.Expand方法展开TreeNode)。 若要展开集合中的所有 Nodes 子树节点级别,请调用 ExpandAll 该方法。 可以通过调用TreeNode.Collapse方法折叠子TreeNode级别,或者用户可以按减号 (-) 按钮(如果旁边TreeNode显示一个)。 还可以调用该方法 TreeNode.Toggle 以在展开状态和折叠状态之间交替。
树节点可以选择显示复选框。 若要显示复选框,请将属性TreeView设置为 CheckBoxes true
。 此属性 Checked 设置为 true
处于选中状态的树节点。
从TreeNode.CheckedBeforeCheck或AfterCheck事件中设置属性会导致事件多次引发,并可能导致意外行为。 例如,在递归更新子节点时,可以在事件处理程序中设置 Checked 属性,以便用户不必单独展开和检查每个节点。 若要防止多次引发该事件,请将逻辑添加到事件处理程序中,该事件处理程序仅当属性未设置为TreeViewAction.Unknown时执行递归代码。Action TreeViewEventArgs 有关如何执行此操作的示例,请参阅或BeforeCheck事件的“示例”部分AfterCheck。
可以通过设置控件的某些 TreeView 显示和样式属性来更改控件的外观。 设置 ShowPlusMinus 可 true
分别显示可展开或折叠的每个 TreeNode 加号或减号按钮。 将 ShowRootLines 属性设置为 true
导致 TreeView 显示将所有根树节点联接在一起的行。 You can display lines that connect child tree nodes to their root node by setting the ShowLines property to true
. 设置属性 HotTracking 以 true
更改树节点标签的外观,因为鼠标指针将鼠标指针传递到树节点标签上。 当热跟踪时,树节点标签会呈现超链接的外观。 还可以完全自定义控件的外观 TreeView 。 为此,请将 DrawMode 属性设置为除该事件以外的 TreeViewDrawMode.Normal 值并处理事件 DrawNode 。
在运行时设置 CheckBoxes、 Scrollable属性 ImageIndex和 SelectedImageIndex 属性时, TreeView (看到 Control.RecreateHandle) 更新控件的外观) 重新创建句柄。 这会导致所有树节点都折叠,但所选 TreeNode节点除外。