添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
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
Object
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 对象的集合,因此在循环访问集合时,可能会发现很难确定树结构中的位置。 可以使用字符串 PathSeparator 值来确定标签的开始和结束位置 TreeNode ,从而分析 TreeNode.FullPath 字符串。

可以通过将 分配给 ImageList ImageList 属性并引用 中 ImageList Image 索引值来在树节点旁边显示图像,以分配该 Image 。 使用以下属性分配图像:

  • ImageIndex 属性设置为未选择树节点时要显示的 的索引值 Image

  • SelectedImageIndex 属性设置为在选择树节点时要显示的 的索引值 Image

    SelectedImageIndex 属性值引用 ImageIndex 的图像是由分配给 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 属性设置为 。CheckBoxestrue Checked对于处于选中状态的树节点, 属性设置为 true

    TreeNode.Checked从 或 AfterCheck 事件中BeforeCheck设置 属性会导致事件多次引发,并可能导致意外行为。 例如,在以递归方式更新子节点时,可以在事件处理程序中设置 Checked 属性,以便用户不必单独展开和检查每个节点。 若要防止多次引发事件,请将逻辑添加到事件处理程序,该处理程序仅在 的 属性TreeViewEventArgs未设置为 TreeViewAction.UnknownAction执行递归代码。 有关如何执行此操作的示例,请参阅 或 BeforeCheck 事件的“示例”部分AfterCheck

    可以通过设置控件的 TreeView 一些显示和样式属性来更改控件的外观。 设置为 ShowPlusMinustrue 时,在每个 TreeNode 可分别展开或折叠的按钮旁边显示一个加号或减号按钮。 将 ShowRootLines 属性设置为 true 会导致 TreeView 显示将所有根树节点联接在一起的行。 通过将 属性true设置为 ,可以显示将子树节点连接到其根节点的ShowLines行。 HotTracking将 属性设置为 true 会更改树节点标签的外观,因为鼠标指针会越过树节点标签。 热跟踪时,树节点标签将呈现超链接的外观。 还可以完全自定义控件的外观 TreeView 。 为此,请将 DrawMode 属性设置为除 TreeViewDrawMode.Normal 以外的值,并处理 DrawNode 事件。

    在运行时设置 CheckBoxesScrollableImageIndexSelectedImageIndex 属性时, TreeView 将重新创建句柄, (查看 Control.RecreateHandle 更新控件外观) 。 这会导致所有树节点都折叠,选定的 TreeNode除外。

  •