{"id":3551,"date":"2011-11-23T11:06:00","date_gmt":"2011-11-23T19:06:00","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=3551"},"modified":"2011-11-23T15:03:14","modified_gmt":"2011-11-23T23:03:14","slug":"primalforms-2011-spotlight-on-the-treeview-control-part-2","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2011\/11\/23\/primalforms-2011-spotlight-on-the-treeview-control-part-2\/","title":{"rendered":"PrimalForms 2011: Spotlight on the TreeView Control &#8211; Part 2"},"content":{"rendered":"<p><em>The \u201cSpotlight on Controls\u201d series focuses on a single WinForms control in PrimalForms 2011, details the important Properties, Methods, and Events of the control and demonstrates how to utilize the control. Most of the information about the controls is still applicable to previous versions of PrimalForms.<\/em><\/p>\n<p>In <a href=\"http:\/\/www.sapien.com\/blog\/2011\/11\/14\/primalforms-2011-spotlight-on-the-treeview-control-part-1\/\">Part 1<\/a> of the Spotlight on the TreeView control, we looked at the TreeView properties, methods and event. Now we will focus on the TreeNodes and the TreeView\u2019s Nodes property.<\/p>\n<p><strong>TreeView Nodes Property Revisited:<\/strong><\/p>\n<p>The TreeView keeps track of its nodes via the <strong>Nodes<\/strong> property, which consists of a collection of TreeNode objects. <\/p>\n<p>This property can be used to remove nodes and\/or add nodes to the root of the tree. <\/p>\n<p><strong>Adding Nodes:<\/strong><\/p>\n<p>You can add nodes via the designer or dynamically via the script editor.<\/p>\n<p><strong><em>Adding nodes in the designer:<\/em><\/strong><\/p>\n<p>Use the Property Pane to edit the nodes:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"Nodes in PropertyPane\" border=\"0\" alt=\"Nodes in PropertyPane\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/11\/Nodes-in-PropertyPane.png\" width=\"227\" height=\"83\"><\/p>\n<p>Or you can access the TreeNode Editor via the designer menu:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"Nodes in Designer Menu\" border=\"0\" alt=\"Nodes in Designer Menu\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/11\/Nodes-in-Designer-Menu.png\" width=\"365\" height=\"151\"><\/p>\n<p>TreeNode Editor:<\/p>\n<p>The TreeNode Editor allows you to add or remove nodes as well as alter their properties. In addition, nodes can be moved up and down the tree to become roots or child nodes. <\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/11\/TreeNode-Editor.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"TreeNode Editor\" border=\"0\" alt=\"TreeNode Editor\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/11\/TreeNode-Editor_thumb.png\" width=\"244\" height=\"151\"><\/a><\/p>\n<p><strong><em>Adding Nodes in Script Editor:<\/em><\/strong><\/p>\n<p>The TreeView\u2019s <strong>Nodes<\/strong> property has several methods that allow you to add nodes dynamically:<\/p>\n<p><font color=\"#0000ff\">[TreeNode]<\/font>&nbsp;<font color=\"#4bacc6\">Add<\/font> (<font color=\"#0000ff\">[string]<\/font> text)<\/p>\n<p><font color=\"#0000ff\">[TreeNode] <\/font><font color=\"#4bacc6\">Add<\/font> (<font color=\"#0000ff\">[TreeNode]<\/font> node)<\/p>\n<p><font color=\"#0000ff\">[TreeNode] <\/font><font color=\"#4bacc6\">Add<\/font> (<font color=\"#0000ff\">[string]<\/font> key, <font color=\"#0000ff\">[string]<\/font> text)<\/p>\n<p><font color=\"#0000ff\">[TreeNode] <\/font><font color=\"#4bacc6\">Add<\/font> (<font color=\"#0000ff\">[string]<\/font> key, <font color=\"#0000ff\">[string]<\/font>text, <font color=\"#0000ff\">[int]<\/font> imageIndex)<\/p>\n<p><font color=\"#0000ff\">[TreeNode]<\/font> <font color=\"#4bacc6\">Add<\/font> (<font color=\"#0000ff\">[string]<\/font> key, <font color=\"#0000ff\">[string]<\/font> text, <font color=\"#0000ff\">[string]<\/font> imageKey)<\/p>\n<p><font color=\"#0000ff\">[TreeNode] <\/font><font color=\"#4bacc6\">Add<\/font> (<font color=\"#0000ff\">[string]<\/font> key, <font color=\"#0000ff\">[string]<\/font> text, <font color=\"#0000ff\">[int]<\/font> imageIndex, <font color=\"#0000ff\">[int] <\/font>selectedImageIndex)<\/p>\n<p><font color=\"#0000ff\">[TreeNode]<\/font> <font color=\"#4bacc6\">Add<\/font> (<font color=\"#0000ff\">[string]<\/font> key, <font color=\"#0000ff\">[string]<\/font> text, <font color=\"#0000ff\">[string]<\/font> imageKey, <font color=\"#0000ff\">[string]<\/font> selectedImageKey)<\/p>\n<p>Example:<\/p>\n<pre><span style=\"color: #8b0000\">$buttonAddNode_Click<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">{\n    <\/span><span style=\"color: #008000\">#Add and Select Node<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #8b0000\">$treeview1<\/span><span style=\"color: #000000\">.SelectedNode <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$treeview1<\/span><span style=\"color: #000000\">.Nodes.Add(<\/span><span style=\"color: #ff0000\"><span style=\"color: #ff0000\">'<\/span>New Node<span style=\"color: #ff0000\">'<\/span><\/span><span style=\"color: #000000\">);\n}<\/span><\/pre>\n<p>You can also use the Insert method to insert a node into a particular position:<\/p>\n<p><font color=\"#0000ff\">[TreeNode] <\/font><font color=\"#4bacc6\">Insert <\/font>(<font color=\"#0000ff\">[int]<\/font> index, <font color=\"#0000ff\">[string]<\/font> text)<\/p>\n<p><font color=\"#0000ff\">[TreeNode] <\/font><font color=\"#4bacc6\">Insert <\/font>(<font color=\"#0000ff\">[int]<\/font> index, <font color=\"#0000ff\">[TreeNode]<\/font> node)<\/p>\n<p><font color=\"#0000ff\">[TreeNode] <\/font><font color=\"#4bacc6\">Insert <\/font>(<font color=\"#0000ff\">[int]<\/font> index, <font color=\"#0000ff\">[string]<\/font> key, <font color=\"#0000ff\">[string]<\/font> text)<\/p>\n<p><font color=\"#0000ff\">[TreeNode] <\/font><font color=\"#4bacc6\">Insert <\/font>(<font color=\"#0000ff\">[int]<\/font> index, <font color=\"#0000ff\">[string]<\/font> key, <font color=\"#0000ff\">[string]<\/font> text, <font color=\"#0000ff\">[int]<\/font> imageIndex)<\/p>\n<p><font color=\"#0000ff\">[TreeNode] <\/font><font color=\"#4bacc6\">Insert <\/font>(<font color=\"#0000ff\">[int]<\/font> index, <font color=\"#0000ff\">[string]<\/font> key, <font color=\"#0000ff\">[string]<\/font> text, <font color=\"#0000ff\">[string]<\/font> imageKey)<\/p>\n<p><font color=\"#0000ff\">[TreeNode] <\/font><font color=\"#4bacc6\">Insert <\/font>(<font color=\"#0000ff\">[int]<\/font> index, <font color=\"#0000ff\">[string]<\/font> key, <font color=\"#0000ff\">[string] <\/font>text, <font color=\"#0000ff\">[int] <\/font>imageIndex, <font color=\"#0000ff\">[int] <\/font>selectedImageIndex)<\/p>\n<p><font color=\"#0000ff\">[TreeNode]<\/font> <font color=\"#4bacc6\">Insert <\/font>(<font color=\"#0000ff\">[int]<\/font> index, <font color=\"#0000ff\">[string]<\/font> key, <font color=\"#0000ff\">[string]<\/font> text, <font color=\"#0000ff\">[string]<\/font> imageKey, <font color=\"#0000ff\">[string]<\/font> selectedImageKey)<\/p>\n<p>Example:<\/p>\n<pre><span style=\"color: #8b0000\">$buttonAddNode_Click<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">{\n    <\/span><span style=\"color: #008000\">#Insert node to the top of the tree<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #8b0000\">$treeview1<\/span><span style=\"color: #000000\">.Nodes.Insert(<\/span><span style=\"color: #000000\">0<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #ff0000\">'New Node<span style=\"color: #ff0000\">'<\/span><\/span><span style=\"color: #000000\">);\n}<\/span><\/pre>\n<p>All the add methods return the TreeNode which was added. Each parameter corresponds to a similarly named TreeNode property. The only exception is the Key parameter, which pertains to the Name property of the TreeNode.<\/p>\n<p><strong>TIP:<\/strong> When making significant changes to the TreeView\u2019s nodes, it is recommended to use the TreeView\u2019s <strong>BeginUpdate<\/strong> and&nbsp; <strong>EndUpdate<\/strong> methods. The <strong>BeginUpdate<\/strong> method prevents the TreeView from redrawing every time a node is added or removed.<\/p>\n<p>Example use of <strong>BeginUpdate<\/strong> and <strong>EndUpdate<\/strong> methods:<\/p>\n<pre><span style=\"color: #008000\">#Disable Drawing<\/span><span style=\"color: #000000\">\n<\/span><span style=\"color: #8b0000\">$treeviewServices<\/span><span style=\"color: #000000\">.BeginUpdate()\n\n<\/span><span style=\"color: #8b0000\">$services<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff; font-weight: bold\">Get-Service<\/span><span style=\"color: #000000\">\n<\/span><span style=\"color: #8b0000\">$treeviewServices<\/span><span style=\"color: #000000\">.Nodes.Clear()\n\n<\/span><span style=\"color: #0000ff\">foreach<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$service<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">in<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$services<\/span><span style=\"color: #000000\">)\n{\n    <\/span><span style=\"color: #8b0000\">$node<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$Root<\/span><span style=\"color: #000000\">.Nodes.Add(<\/span><span style=\"color: #8b0000\">$service<\/span><span style=\"color: #000000\">.DisplayName)    \n}\n<\/span><span style=\"color: #008000\">#Enable Drawing<\/span><span style=\"color: #000000\">\n<\/span><span style=\"color: #8b0000\">$treeviewServices<\/span><span style=\"color: #000000\">.EndUpdate()<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong><em>Removing Nodes:<\/em><\/strong><\/p>\n<p>Use the Remove method to remove any node:<\/p>\n<p><font color=\"#0000ff\">[void]<\/font> <font color=\"#4bacc6\">Remove <\/font>(<font color=\"#0000ff\">[TreeNode] <\/font>node)<\/p>\n<p>Example:<\/p>\n<pre><span style=\"color: #8b0000\">$buttonRemoveNode_Click<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">{\n    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$treeview1<\/span><span style=\"color: #000000\">.SelectedNode <\/span><span style=\"color: #0000ff\">-ne<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$null<\/span><span style=\"color: #000000\">)\n    {\n        <\/span><span style=\"color: #008000\">#Remove the selected node<\/span><span style=\"color: #000000\">\n        <\/span><span style=\"color: #8b0000\">$treeview1<\/span><span style=\"color: #000000\">.Nodes.Remove(<\/span><span style=\"color: #8b0000\">$treeview1<\/span><span style=\"color: #000000\">.SelectedNode)    \n    }    \n}<\/span><\/pre>\n<p>The following method removes all the nodes in the tree:<\/p>\n<p><font color=\"#0000ff\">[void]<\/font> <font color=\"#4bacc6\">Clear <\/font>()<\/p>\n<p>Example:<\/p>\n<pre><span style=\"color: #8b0000\">$buttonRemoveAllNode_Click<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">{\n    <\/span><span style=\"color: #8b0000\">$treeview1<\/span><span style=\"color: #000000\">.Nodes.Clear()\n}<\/span><\/pre>\n<p><strong><em>Searching for Nodes:<\/em><\/strong><\/p>\n<p>If you wish to find a node in the tree, you can use the <strong>Nodes<\/strong> property\u2019s Find method:<\/p>\n<p><font color=\"#0000ff\">[TreeNode []]<\/font> <font color=\"#4bacc6\">Find <\/font>(<font color=\"#0000ff\">[string]<\/font> key, <font color=\"#0000ff\">[bool]<\/font> searchAllChildren)<\/p>\n<p>Example:<\/p>\n<pre><span style=\"color: #000000\">    <\/span><span style=\"color: #8b0000\">$foundNodes<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$treeview1<\/span><span style=\"color: #000000\">.Nodes.Find(<\/span><span style=\"color: #ff0000\">\"Node\"<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #8b0000\">$true<\/span><span style=\"color: #000000\">)\n    \n    <\/span><span style=\"color: #0000ff\">foreach<\/span><span style=\"color: #000000\"> (<\/span><span style=\"color: #8b0000\">$node<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">in<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$foundNodes<\/span><span style=\"color: #000000\">) {\n        <\/span><span style=\"color: #0000ff; font-weight: bold\">Write-Host<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">\"Found: \"<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$node<\/span><span style=\"color: #000000\">.Text\n    }<\/span><\/pre>\n<p>The Find method performs a case insensitive search of&nbsp; the TreeNodes\u2019&nbsp; Name property to find a match. Therefore it is important to note that it does not search the Text property, so if the Name property is empty, the method will not find the node.<\/p>\n<p>&nbsp;<\/p>\n<p>Let\u2019s look at the TreeNode object and its important properties and methods:<\/p>\n<p><span style=\"font-size: medium\"><strong>TreeNode <\/strong>[System.Windows.Forms.TreeNode]<\/span><\/p>\n<p>Represents a node of a TreeView.<\/p>\n<p><strong><span style=\"font-size: small\"><font size=\"3\"><\/font><\/span><\/strong><\/p>\n<p><span style=\"font-size: small\"><font size=\"3\"><strong>Important Properties:<\/strong><\/font><\/span> <\/p>\n<p><strong>Checked<\/strong> <\/p>\n<p>This property gets or sets a value indicating whether the tree node is in a checked state. In order to see the check box, the TreeView must have its <strong>CheckBoxes<\/strong> property set to True. <\/p>\n<p>To determine if a Checked property has changed, use the TreeView\u2019s AfterChecked event. The event can be useful for tracking checked nodes. Another method of determining which TreeNodes are checked is to iterate through all the TreeNodes and inspect the value of the Checked property. <\/p>\n<p>Sample Function that returns all the checked nodes:<\/p>\n<pre><span style=\"color: #0000ff\">function<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #008080\">Get-CheckedNodes<\/span><span style=\"color: #000000\"> \n{\n    <\/span><span style=\"color: #0000ff\">param<\/span><span style=\"color: #000000\">(\n    [ValidateNotNull()]\n    [System.Windows.Forms.TreeNodeCollection] <\/span><span style=\"color: #8b0000\">$NodeCollection<\/span><span style=\"color: #000000\">,\n    [ValidateNotNull()]\n    [System.Collections.ArrayList]<\/span><span style=\"color: #8b0000\">$CheckedNodes<\/span><span style=\"color: #000000\">)\n    \n    <\/span><span style=\"color: #0000ff\">foreach<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$Node<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">in<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$NodeCollection<\/span><span style=\"color: #000000\">)\n    {\n        <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$Node<\/span><span style=\"color: #000000\">.Checked)\n        {\n            [void]<\/span><span style=\"color: #8b0000\">$CheckedNodes<\/span><span style=\"color: #000000\">.Add(<\/span><span style=\"color: #8b0000\">$Node<\/span><span style=\"color: #000000\">)\n        }\n        <\/span><span style=\"color: #008080\">Get-CheckedNodes<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$Node<\/span><span style=\"color: #000000\">.Nodes <\/span><span style=\"color: #8b0000\">$CheckedNodes<\/span><span style=\"color: #000000\">\n    }\n}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p>Calling the function:<\/p>\n<pre><span style=\"color: #000000\">    <\/span><span style=\"color: #8b0000\">$CheckedNodes<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff; font-weight: bold\">New-Object<\/span><span style=\"color: #000000\"> System.Collections.ArrayList\n    <\/span><span style=\"color: #008080\">Get-CheckedNodes<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$treeview1<\/span><span style=\"color: #000000\">.Nodes <\/span><span style=\"color: #8b0000\">$CheckedNodes<\/span><span style=\"color: #000000\">\n    \n    <\/span><span style=\"color: #0000ff\">foreach<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$node<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">in<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$CheckedNodes<\/span><span style=\"color: #000000\">)\n    {    \n        <\/span><span style=\"color: #0000ff; font-weight: bold\">Write-Host<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$node<\/span><span style=\"color: #000000\">.Text\n    }<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong>ContextMenuStrip<\/strong> <\/p>\n<p>This property gets or sets the shortcut menu associated with this tree node. <\/p>\n<p>Use this property to assign a specific ContextMenuStrip to each individual node.&nbsp; Each node can share the same ContextMenuStrip or have a unique ContextMenuStrip assigned to it. <\/p>\n<p><strong>FirstNode<\/strong> <\/p>\n<p>This property returns the first child tree node in the tree node collection. <\/p>\n<p>This property can be useful for navigating through the nodes. <\/p>\n<p><strong>FullPath<\/strong> <\/p>\n<p>This property returns the path from the root tree node to the current tree node. This property can be useful for navigation purposes. <\/p>\n<p>Example: <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"AfterSort\" border=\"0\" alt=\"AfterSort\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/11\/AfterSort1.png\" width=\"119\" height=\"126\"> <\/p>\n<p>Node \u201c1 Nodes\u201d FullPath is: \u201cRoot\\B Node\\1 Node&#8221; <\/p>\n<p>You can change the backslash character \u2018\\\u2019 to any other character or string by changing the TreeView\u2019s PathSeparator property. <\/p>\n<p><strong>IsEditing<\/strong> <\/p>\n<p>This property gets a value indicating whether the tree node is in an editable state. <\/p>\n<p><strong>IsExpanded<\/strong> <\/p>\n<p>This property gets a value indicating whether the tree node is in the expanded state. <\/p>\n<p><strong>IsSelected<\/strong> <\/p>\n<p>This property gets a value indicating whether the tree node is in the selected state. <\/p>\n<p><strong>Name<\/strong> <\/p>\n<p>This property gets or sets the name of the tree node.&nbsp; Note: The Name property is not the same as the Text property. When using the Find method, it will use this property to perform the search. <\/p>\n<p><strong>NextNode<\/strong> <\/p>\n<p>This property gets the next sibling tree node. <\/p>\n<p>This property can be useful for navigating through the nodes.<\/p>\n<pre><span style=\"color: #8b0000\">$node<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$treeView1<\/span><span style=\"color: #000000\">.Nodes[<\/span><span style=\"color: #000000\">0<\/span><span style=\"color: #000000\">]\n\n<\/span><span style=\"color: #0000ff\">while<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$node<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-ne<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$null<\/span><span style=\"color: #000000\">)\n{\n    <\/span><span style=\"color: #0000ff; font-weight: bold\">Write-Host<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$node<\/span><span style=\"color: #000000\">.Name  \n    <\/span><span style=\"color: #8b0000\">$node<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$node<\/span><span style=\"color: #000000\">.NextNode\n}<\/span><\/pre>\n<p>&nbsp; <\/p>\n<p><strong>Nodes<\/strong> <\/p>\n<p>This property get the collection of child TreeNode objects assigned to the current tree node.&nbsp; <\/p>\n<p>This property is identical to the TreeView\u2019s Nodes property, but instead contains this node\u2019s children.&nbsp; <\/p>\n<p><strong>PrevNode<\/strong> <\/p>\n<p>This property gets the previous sibling tree node.&nbsp; <\/p>\n<p>Like the NextNode property, this property can be useful for navigating through the nodes. <\/p>\n<p><strong>ImageIndex<\/strong> <\/p>\n<p>This property gets or sets the image list index value of the image displayed when the tree node is in the unselected state. <\/p>\n<p>TreeView\u2019s ImageList property must be set for this property to function correctly. If not set, the node will use the default index determined by the TreeView\u2019s ImageIndex property. <\/p>\n<p>To remove the Image from the node, set the node\u2019s ImageIndex property to \u20131. <\/p>\n<p><strong>SelectedImageIndex<\/strong> <\/p>\n<p>This property gets or sets the image list index value of the image that is displayed when the tree node is in the selected state. <\/p>\n<p>TreeView\u2019s ImageList property must be set for this property to function correctly. If not set, the node will use the default index determined by the TreeView\u2019s SelectedImageIndex property. <\/p>\n<p>To remove the Image from the node, set the node\u2019s SelectedImageIndex property to \u20131. <\/p>\n<p><strong>StateImageIndex<\/strong> <\/p>\n<p>This property gets or sets the index of the image that is used to indicate the state of the TreeNode when the parent TreeView has its CheckBoxes property set to false. <\/p>\n<p>TreeView\u2019s StateImageList property must be set for this property to function correctly. To remove the Image from the node, set the node\u2019s StateImageList property to \u20131. <\/p>\n<p>Example: <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"StateImageIndex\" border=\"0\" alt=\"StateImageIndex\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/11\/StateImageIndex.png\" width=\"130\" height=\"77\"> <\/p>\n<p>In this example the StateImageIndex is used to signify a problem with a file. <\/p>\n<p>Warning: If the TreeView\u2019s CheckBoxes is set to True, the StateImage will be displayed instead of the CheckBox.&nbsp; <\/p>\n<p><strong>Tag<\/strong> <\/p>\n<p>This property gets or sets the object that contains data about the tree node. <\/p>\n<p>This property can be extremely useful for linking the Node to an actual object. By using the Tag property, you can easily reference the original object. For example, if the nodes of a TreeView consists of services and you want to start \/ stop a service by double clicking on the node, you can assign the service object to the node\u2019s Tag property and as a result it simplifies the event handling.<\/p>\n<pre><span style=\"color: #8b0000\">$treeviewServices_NodeMouseDoubleClick<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">[System.Windows.Forms.TreeNodeMouseClickEventHandler]{\n<\/span><span style=\"color: #008000\">#Event Argument: $_ = [System.Windows.Forms.TreeNodeMouseClickEventArgs]<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #8b0000\">$service<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.Node.Tag\n    \n    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$service<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-ne<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$null<\/span><span style=\"color: #000000\">)<span style=\"color: #008000\">#Is there is a service object?<\/span>\n    {\n        <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$service<\/span><span style=\"color: #000000\">.Status <\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">'Running'<\/span><span style=\"color: #000000\">)\n        {\n            <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$service<\/span><span style=\"color: #000000\">.CanPauseAndContinue)\n            {\n                <\/span><span style=\"color: #008000\">#$service.Pause()<\/span><span style=\"color: #000000\">\n                <\/span><span style=\"color: #0000ff; font-weight: bold\">Write-Host<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">'Pause Service'<\/span><span style=\"color: #000000\">\n <\/span><span style=\"color: #000000\">           }\n            <\/span><span style=\"color: #0000ff\">else<\/span><span style=\"color: #000000\">\n            {\n                <\/span><span style=\"color: #008000\">#$service.Stop()<\/span><span style=\"color: #000000\">\n                <\/span><span style=\"color: #0000ff; font-weight: bold\">Write-Host<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">'Stop Service'<\/span><span style=\"color: #000000\">\n<\/span><span style=\"color: #000000\">            }\n        }\n        <\/span><span style=\"color: #0000ff\">elseif<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$service<\/span><span style=\"color: #000000\">.Status <\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">'Paused'<\/span><span style=\"color: #000000\">)\n        {\n            <\/span><span style=\"color: #008000\">#$service.Continue()<\/span><span style=\"color: #000000\">\n            <\/span><span style=\"color: #0000ff; font-weight: bold\">Write-Host<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">'Continue Service'<\/span><span style=\"color: #000000\">\n<\/span><span style=\"color: #000000\">        }\n        <\/span><span style=\"color: #0000ff\">else<\/span><span style=\"color: #000000\">\n        {\n            <\/span><span style=\"color: #008000\">#$service.Start()    <\/span><span style=\"color: #000000\">\n            <\/span><span style=\"color: #0000ff; font-weight: bold\">Write-Host<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">'Start Service'<\/span><span style=\"color: #000000\">\n<\/span><span style=\"color: #000000\">        }\n    }\n    \n}<\/span><\/pre>\n<p><strong><\/strong>&nbsp; <\/p>\n<p><strong>Text<\/strong> <\/p>\n<p>This property gets or sets the text displayed in the label of the tree node. <\/p>\n<p><strong>ToolTipText<\/strong> <\/p>\n<p>This property gets or sets the text that appears when the mouse pointer hovers over a TreeNode. The TreeView\u2019s ShowNodeTooltips property must be set to True in order for the tooltips to display.<\/p>\n<pre><span style=\"color: #8b0000\">$node<\/span><span style=\"color: #000000\">.ToolTipText <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">'This node has a tooltip!'<\/span>&nbsp;<\/pre>\n<pre>&nbsp;<\/pre>\n<p><strong><font size=\"3\">Methods:<\/font><\/strong> <\/p>\n<p><strong>BeginEdit<\/strong> <\/p>\n<p>This method initiates the editing of the tree node label. <\/p>\n<pre><span style=\"color: #8b0000\">$buttonRename_Click<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">{\n    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$treeview1<\/span><span style=\"color: #000000\">.SelectedNode <\/span><span style=\"color: #0000ff\">-ne<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$null<\/span><span style=\"color: #000000\">)\n    {\n        <\/span><span style=\"color: #8b0000\">$treeview1<\/span><span style=\"color: #000000\">.SelectedNode.BeginEdit()\n    }\n}<\/span><\/pre>\n<p><strong><\/strong>&nbsp; <\/p>\n<p><strong>Collapse<\/strong> <\/p>\n<p>This method collapses the TreeNode and optionally collapses its children. <\/p>\n<pre><span style=\"color: #008000\">#Collapse All Children<\/span><span style=\"color: #000000\">\n<\/span><span style=\"color: #8b0000\">$treeview1<\/span><span style=\"color: #000000\">.SelectedNode.Collapse()\n\n<\/span><span style=\"color: #008000\">#Collapse only this node and leave children<\/span><span style=\"color: #000000\">\n<\/span><span style=\"color: #8b0000\">$treeview1<\/span><span style=\"color: #000000\">.SelectedNode.Collapse(<\/span><span style=\"color: #8b0000\">$false<\/span><span style=\"color: #000000\">)<\/span><\/pre>\n<p><strong>EndEdit<\/strong> <\/p>\n<p>This method ends the editing of the tree node label. <\/p>\n<pre><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$treeview1<\/span><span style=\"color: #000000\">.SelectedNode.IsEditing)\n{\n    <\/span><span style=\"color: #8b0000\">$treeview1<\/span><span style=\"color: #000000\">.SelectedNode.EndEdit()    \n}<\/span><\/pre>\n<p><strong><\/strong>&nbsp; <\/p>\n<p><strong>EnsureVisible<\/strong> <\/p>\n<p>This method ensures that the tree node is visible, expanding tree nodes and scrolling the tree view control as necessary. <\/p>\n<p><span style=\"color: #8b0000\">$treeview1<\/span><span style=\"color: #000000\">.SelectedNode.EnsureVisible() <\/span><\/p>\n<p><strong>Expand<\/strong> <\/p>\n<p>This method expands the tree node. <\/p>\n<p><span style=\"color: #8b0000\">$treeview1<\/span><span style=\"color: #000000\">.SelectedNode.Expand() <\/span><\/p>\n<p><strong>ExpandAll<\/strong> <\/p>\n<p>This method expands all the child tree nodes. <\/p>\n<p><span style=\"color: #8b0000\">$treeview1<\/span><span style=\"color: #000000\">.SelectedNode.ExpandAll() <\/span><\/p>\n<p><strong>Remove<\/strong> <\/p>\n<p>This method removes the current tree node from the tree view control. <\/p>\n<p><span style=\"color: #8b0000\">$treeview1<\/span><span style=\"color: #000000\">.SelectedNode.Remove() <\/span><\/p>\n<p><strong>Toggle<\/strong> <\/p>\n<p>This method toggles the tree node to either the expanded or collapsed state. <\/p>\n<p><span style=\"color: #8b0000\">$treeview1<\/span><span style=\"color: #000000\">.SelectedNode.Toggle() <\/span><\/p>\n<p>&nbsp; <\/p>\n<p><strong>&nbsp;<\/strong>Download the <a href=\"http:\/\/www.sapien.com\/downloads#Sample Scripts\/PrimalForms 2011\/TreeViewSamples.zip\">TreeView Samples<\/a> from the <a href=\"http:\/\/sapien.com\/downloads\">downloads<\/a> section.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The \u201cSpotlight on Controls\u201d series focuses on a single WinForms control in PrimalForms 2011, details the important Properties, Methods, and Events of the control and demonstrates how to utilize the control. Most of the information about the controls is still applicable to previous versions of PrimalForms. In Part 1 of the Spotlight on the TreeView [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[509,575,644,25],"tags":[641,28,633,490,327],"class_list":["post-3551","post","type-post","status-publish","format-standard","hentry","category-primalforms-software-news","category-primalforms-2011-software-news","category-spotlight-on-controls","category-windows-powershell","tag-controls","tag-powershell","tag-primalforms-2011","tag-treeview","tag-winform"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3551","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=3551"}],"version-history":[{"count":12,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3551\/revisions"}],"predecessor-version":[{"id":3600,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3551\/revisions\/3600"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=3551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=3551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=3551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}