{"id":3544,"date":"2011-11-14T11:10:00","date_gmt":"2011-11-14T19:10:00","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=3544"},"modified":"2016-01-25T21:00:39","modified_gmt":"2016-01-26T05:00:39","slug":"primalforms-2011-spotlight-on-the-treeview-control-part-1","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2011\/11\/14\/primalforms-2011-spotlight-on-the-treeview-control-part-1\/","title":{"rendered":"PrimalForms 2011: Spotlight on the TreeView Control &ndash; Part 1"},"content":{"rendered":"<p>The <em><a href=\"https:\/\/www.sapien.com\/blog\/topics\/spotlight-on-controls\/\" target=\"_blank\">Spotlight on Controls<\/a><\/em> series describes the <i>controls<\/i>, that is, the objects in the System.Windows.Forms namespace, so you can use them effectively when building GUI apps in PowerShell Studio and PrimalScript.<\/p>\n<p>Each post focuses on one control and lists its most important properties, methods, and events, including the default event that PowerShell Studio adds to your script when you double-click the control. The posts include many examples written in Windows PowerShell, so you can use them right away.<\/p>\n<p>Read more: <a href=\"https:\/\/www.sapien.com\/blog\/2011\/06\/01\/primalforms-2011-spotlight-on-controls\/\">Spotlight on Controls: Common properties and methods<\/a><\/p>\n<p>This two-part\u00a0post describes the TreeView control.<\/p>\n<ul>\n<li><strong>Part 1: <\/strong>TreeView Overview<\/li>\n<li><strong>Part 2:<\/strong> TreeNode Overview<\/li>\n<\/ul>\n<h1>TreeView [System.Windows.Forms.TreeView]<\/h1>\n<p>Displays a hierarchical collection of labeled items, each of which is represented by a TreeNode.<\/p>\n<ul>\n<li>MSDN Page: <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.windows.forms.treeview(v=vs.110).aspx\" target=\"_blank\">System.Windows.Forms.TreeView<\/a><\/li>\n<li>Default Event: <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.windows.forms.treeview.afterselect(v=vs.110).aspx\">AfterSelect<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;\" title=\"TreeView Toolbox Item\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/11\/TreeView-Toolbox-Item.png\" alt=\"TreeView Toolbox Item\" width=\"107\" height=\"21\" border=\"0\" \/><\/p>\n<p>Default Event: AfterSelect<\/p>\n<p><em>Why use a TreeView control?<\/em><\/p>\n<p>When it comes to displaying hierarchical information such as a folder structure, the TreeView control is a perfect choice. In addition, the TreeView allows users to rename tree nodes which can be useful in many instances. The TreeView also serves as a good navigation tool.<\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-size: medium;\"><strong>Important Properties:<\/strong><\/span><\/span><\/p>\n<p><strong>CheckBoxes<\/strong><\/p>\n<p>This property indicates whether check boxes are displayed next to the tree nodes in the tree view control.<\/p>\n<p>This property can be useful if you want the user to pick multiple nodes by checking them. Note: Checked nodes are not treated as if they are \u201cselected\u201d. We will cover how to determine which nodes are checked by the user in Part 2.<\/p>\n<p>Default Value: False<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;\" title=\"Nodes with CheckBoxes\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/11\/Nodes-with-CheckBoxes.png\" alt=\"Nodes with CheckBoxes\" width=\"140\" height=\"83\" border=\"0\" \/><\/p>\n<p><strong>ContextMenuStrip<\/strong><\/p>\n<p>This property sets the shortcut menu associated with the control.<\/p>\n<p>Use this property to select an existing ContextMenuStrip.The ContextMenuStrip will be featured in a future Spotlight on Control article. The context menu will appear when TreeView is right clicked. Part 2 will cover how to add specific context menus for nodes.<\/p>\n<p><strong>HideSelection<\/strong><\/p>\n<p>This property indicates whether the selected tree node remains highlighted even when the tree view has lost the focus such as when another control is selected.<\/p>\n<p>Default Value: <em>True<\/em><\/p>\n<p>Selection with Focus:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;\" title=\"SelectionWithFocus\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/11\/SelectionWithFocus.png\" alt=\"SelectionWithFocus\" width=\"118\" height=\"68\" border=\"0\" \/><\/p>\n<p>Selection without Focus and HideSelection is set to <em>False<\/em>:<\/p>\n<p><em><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;\" title=\"HideSelection Disabled\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/11\/HideSelection-Disabled.png\" alt=\"HideSelection Disabled\" width=\"124\" height=\"68\" border=\"0\" \/><\/em><\/p>\n<p>&nbsp;<\/p>\n<p><strong>ImageList<\/strong><\/p>\n<p>This property\u00a0 sets the ImageList that contains the Image objects that are used by the tree nodes. (See the <a href=\"primalforms-2011-spotlight-on-the-imagelist-control\">Spotlight on the ImageList Control<\/a> blog article for more details.)<\/p>\n<p><strong>ImageIndex<\/strong><\/p>\n<p>This property indicates the default image index value that is displayed by the tree nodes.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;\" title=\"ImageIndex\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/11\/ImageIndex.png\" alt=\"ImageIndex\" width=\"158\" height=\"67\" border=\"0\" \/><\/p>\n<p><strong>SelectedImageIndex<\/strong><\/p>\n<p>This property indicates the default image index value that is displayed when a tree node is selected.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;\" title=\"SelectedImageIndex\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/11\/SelectedImageIndex.png\" alt=\"SelectedImageIndex\" width=\"144\" height=\"66\" border=\"0\" \/><\/p>\n<p><strong>StateImageList<\/strong><\/p>\n<p>This property sets the ImageList that is used to indicate the state of the TreeView and its nodes.\u00a0 This image is displayed next to the imaged specified by the ImageIndex and SelectedImageIndex. See Part 2 for more details.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>LabelEdit<\/strong><\/p>\n<p>This property indicates whether the label text of the tree nodes can be edited. This can be useful when you wish to allow the user to rename an object such as a file.<\/p>\n<p>Default Value: <em>False<\/em><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;\" title=\"LabelEdit\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/11\/LabelEdit.png\" alt=\"LabelEdit\" width=\"148\" height=\"70\" border=\"0\" \/><\/p>\n<p>When a user edits a label, various events are called that allow you to prevent or reject the changed label. See the BeforeLabeEdit and AfterLabelEdit events before for more details.<\/p>\n<p><strong>ShowLines<\/strong><\/p>\n<p>This property indicates whether lines are drawn between tree nodes in the tree view control.<\/p>\n<p>Default Value: <em>True<\/em><\/p>\n<p>The lines are hidden when ShowLines is set to <em>False:<\/em><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;\" title=\"ShowLines Disabled\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/11\/ShowLines-Disabled.png\" alt=\"ShowLines Disabled\" width=\"118\" height=\"59\" border=\"0\" \/><\/p>\n<p><strong>ShowNodeToolTips<\/strong><\/p>\n<p>This property indicates whether ToolTips are shown when the mouse pointer hovers over a TreeNode. Displaying tooltips can be helpful in providing users with hints about what the node represents.<\/p>\n<p>Default Value: <em>False<\/em><\/p>\n<p>Refer to Part 2 to see how to set a node\u2019s tooltip.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;\" title=\"Node ToolTip\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/11\/Node-ToolTip.png\" alt=\"Node ToolTip\" width=\"159\" height=\"88\" border=\"0\" \/><\/p>\n<p><strong>ShowPlusMinus<\/strong><\/p>\n<p>This property indicates whether plus-sign (+) and minus-sign (-) buttons are displayed next to tree nodes that contain child tree nodes.<\/p>\n<p>Default Value: <em>True<\/em><\/p>\n<p>When ShowPlusMinus is set to False, the plus\/minus buttons are hidden but you can still expand and collapse the node by double clicking on it. To disable this behavior please refer to the BeforeExpand \/ BeforeCollapse events below.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;\" title=\"ShowPlusMinus Disabled\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/11\/ShowPlusMinus-Disabled.png\" alt=\"ShowPlusMinus Disabled\" width=\"131\" height=\"71\" border=\"0\" \/><\/p>\n<p><strong>ShowRootLines<\/strong><\/p>\n<p>This property indicates whether lines are drawn between the tree nodes that are at the root of the tree view.<\/p>\n<p>Default Value: <em>True<\/em><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;\" title=\"ShowRootLines Disabled\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/11\/ShowRootLines-Disabled.png\" alt=\"ShowRootLines Disabled\" width=\"113\" height=\"79\" border=\"0\" \/><\/p>\n<p>When the ShowRootLines is set to False, the root node does not display either a line or the plus or minus button.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Nodes<\/strong><\/p>\n<p>This property contains the collection of tree nodes that are assigned to the tree view control.<\/p>\n<p>Use this property to add or remove nodes. Part 2 will examine this property in more detail.<\/p>\n<p><strong>SelectedNode<\/strong><\/p>\n<p>This property get or sets the tree node that is currently selected in the tree view control.<\/p>\n<p>Example of how to select the first root node via Script Editor:<\/p>\n<pre><span style=\"color: #8b0000;\">$treeview1<\/span><span style=\"color: #000000;\">.SelectedNode <\/span><span style=\"color: #0000ff;\">=<\/span> <span style=\"color: #8b0000;\">$treeview1<\/span><span style=\"color: #000000;\">.Nodes[<\/span><span style=\"color: #000000;\">0<\/span><span style=\"color: #000000;\">]<\/span><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;\" title=\"SelectNode\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/11\/SelectNode.png\" alt=\"SelectNode\" width=\"119\" height=\"61\" border=\"0\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong><span style=\"font-size: medium;\">Important Methods:<\/span><\/strong><\/p>\n<p><strong>CollapseAll<\/strong><\/p>\n<p>This method collapses all the tree nodes.<\/p>\n<pre><span style=\"color: #8b0000;\">$treeview1<\/span><span style=\"color: #000000;\">.CollapseAll()<\/span><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;\" title=\"CollapseAll\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/11\/CollapseAll.png\" alt=\"CollapseAll\" width=\"108\" height=\"65\" border=\"0\" \/><\/p>\n<p><strong>ExpandAll<\/strong><\/p>\n<p>This method expands all the tree nodes.<\/p>\n<pre><span style=\"color: #8b0000;\">$treeview1<\/span><span style=\"color: #000000;\">.ExpandAll()<\/span><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;\" title=\"ExpandAll\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/11\/ExpandAll.png\" alt=\"ExpandAll\" width=\"127\" height=\"77\" border=\"0\" \/><\/p>\n<p><strong>Sort<\/strong><\/p>\n<p>This method sorts all the nodes in TreeView control.<\/p>\n<pre><span style=\"color: #8b0000;\">$treeview1<\/span><span style=\"color: #000000;\">.Sort()<\/span><\/pre>\n<p>Before Sort:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;\" title=\"BeforeSort\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/11\/BeforeSort.png\" alt=\"BeforeSort\" width=\"118\" height=\"126\" border=\"0\" \/><\/p>\n<p>After Sort:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;\" title=\"AfterSort\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/11\/AfterSort.png\" alt=\"AfterSort\" width=\"119\" height=\"126\" border=\"0\" \/><\/p>\n<p><strong><span style=\"font-size: medium;\">Important Events:<\/span><\/strong><\/p>\n<p><strong><em>After Events:<\/em><\/strong><\/p>\n<p>The \u2018After\u2019 events listed below, except AfterLabelEdit, have the following as an argument that is accessible via the <span style=\"color: #8b0000;\">$_<\/span> variable:<\/p>\n<p><strong>[System.Windows.Forms.TreeViewEventArgs]<\/strong><\/p>\n<table border=\"1\" width=\"450\" cellspacing=\"0\" cellpadding=\"2\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"80\"><strong>Properties<\/strong><\/td>\n<td valign=\"top\" width=\"370\"><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"80\">Action<\/td>\n<td valign=\"top\" width=\"370\">Gets the type of action that raised the event.<\/p>\n<p>Values:<\/p>\n<p><em>Unknown<\/em><br \/>\nThe action that caused the event is unknown.<\/p>\n<p><em>ByKeyboard<\/em><br \/>\nThe event was caused by a keystroke.<\/p>\n<p><em>ByMouse<\/em><br \/>\nThe event was caused by a mouse operation.<\/p>\n<p><em>Collapse<\/em><br \/>\nThe event was caused by the TreeNode collapsing.<\/p>\n<p><em>Expand<\/em><br \/>\nThe event was caused by the TreeNode expanding.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"80\">Node<\/td>\n<td valign=\"top\" width=\"370\">Gets the tree node that has been checked, expanded, collapsed, or selected.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p><strong>AfterCheck<\/strong><\/p>\n<p>This event occurs after the tree node check box is checked or unchecked.<\/p>\n<p><strong>AfterCollapse<\/strong><\/p>\n<p>This event occurs after the tree node is collapsed.<\/p>\n<p><strong>AfterExpand<\/strong><\/p>\n<p>This event occurs after the tree node is expanded.<\/p>\n<p><strong>AfterLabelEdit<\/strong><\/p>\n<p>This event occurs after the tree node label text is edited. Label editing must be enabled for this event to be called (See LabelEdit property).<\/p>\n<p>AfterLabelEdit event uses the following argument:<\/p>\n<p><strong>[System.Windows.Forms.NodeLabelEditEventArgs]<\/strong><\/p>\n<table border=\"1\" width=\"452\" cellspacing=\"0\" cellpadding=\"2\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"80\"><strong>Properties<\/strong><\/td>\n<td valign=\"top\" width=\"370\"><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"80\">CancelEdit<\/td>\n<td valign=\"top\" width=\"370\">Gets or sets a value indicating whether the edit has been canceled.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"80\">Label<\/td>\n<td valign=\"top\" width=\"370\">Gets the <em>new<\/em> text to associate with the tree node.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"80\">Node<\/td>\n<td valign=\"top\" width=\"370\">Gets the tree node that has been checked, expanded, collapsed, or selected.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>Use this event to react to name changes. For example if the Node represents a file, you can use this event as a trigger for renaming the file. You should also perform any validation and reject the name change if there is a failure.<\/p>\n<pre><span style=\"color: #8b0000;\">$treeview1_AfterLabelEdit<\/span><span style=\"color: #0000ff;\">=<\/span><span style=\"color: #000000;\">[System.Windows.Forms.NodeLabelEditEventHandler]{\r\n<\/span><span style=\"color: #008000;\">#Event Argument: $_ = [System.Windows.Forms.NodeLabelEditEventArgs]<\/span>\r\n    <span style=\"color: #0000ff;\">if<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #0000ff;\">-not<\/span><span style=\"color: #000000;\"> (<\/span><span style=\"color: #008080;\">Validate-FileName<\/span> <span style=\"color: #8b0000;\">$_<\/span><span style=\"color: #000000;\">.Label))\r\n    {\r\n        <\/span><span style=\"color: #8b0000;\">$_<\/span><span style=\"color: #000000;\">.CancelEdit <\/span><span style=\"color: #0000ff;\">=<\/span> <span style=\"color: #8b0000;\">$true<\/span><span style=\"color: #000000;\">\r\n    }\r\n    <\/span><span style=\"color: #0000ff;\">else<\/span><span style=\"color: #000000;\">\r\n    {\r\n        <\/span><span style=\"color: #008000;\">#Rename the file<\/span><span style=\"color: #000000;\">\r\n    }\r\n}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong>AfterSelect<\/strong><\/p>\n<p>This event occurs after the tree node is selected.<\/p>\n<p>This event is useful if you are using the TreeView as a form of navigation.<\/p>\n<pre><span style=\"color: #8b0000;\">$treeview1_AfterSelect<\/span><span style=\"color: #0000ff;\">=<\/span><span style=\"color: #000000;\">[System.Windows.Forms.TreeViewEventHandler]{\r\n<\/span><span style=\"color: #008000;\">#Event Argument: $_ = [System.Windows.Forms.TreeViewEventArgs]<\/span>\r\n    <span style=\"color: #0000ff; font-weight: bold;\">Write-Host<\/span> <span style=\"color: #ff0000;\">'Node Selected: '<\/span> <span style=\"color: #8b0000;\">$_<\/span><span style=\"color: #000000;\">.Node.Text\r\n}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><em><strong>Before Events:<\/strong><\/em><\/p>\n<p>The\u00a0 \u2018Before\u2019 events listed below, except BeforeLabelEdit, have the following as an argument that is accessible via the <span style=\"color: #8b0000;\">$_<\/span> variable:<\/p>\n<p><strong>[System.Windows.Forms.TreeViewCancelEventArgs]<\/strong><\/p>\n<table border=\"1\" width=\"450\" cellspacing=\"0\" cellpadding=\"2\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"80\"><strong>Properties<\/strong><\/td>\n<td valign=\"top\" width=\"370\"><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"80\">Action<\/td>\n<td valign=\"top\" width=\"370\">Gets the type of action that raised the event.<\/p>\n<p>Values:<\/p>\n<p><em>Unknown<\/em><br \/>\nThe action that caused the event is unknown.<\/p>\n<p><em>ByKeyboard<\/em><br \/>\nThe event was caused by a keystroke.<\/p>\n<p><em>ByMouse<\/em><br \/>\nThe event was caused by a mouse operation.<\/p>\n<p><em>Collapse<\/em><br \/>\nThe event was caused by the TreeNode collapsing.<\/p>\n<p><em>Expand<\/em><br \/>\nThe event was caused by the TreeNode expanding.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"80\">Cancel<\/td>\n<td valign=\"top\" width=\"370\">Gets or sets a value indicating whether the event should be canceled. To cancel the event, set this property to <em>True<\/em>.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"80\">Node<\/td>\n<td valign=\"top\" width=\"370\">Gets the tree node that has been checked, expanded, collapsed, or selected.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p><strong>BeforeCheck<\/strong><\/p>\n<p>This event occurs before the tree node check box is checked.<\/p>\n<p>Use this event to cancel the check action:<\/p>\n<pre><span style=\"color: #8b0000;\">$treeview1_BeforeCheck<\/span><span style=\"color: #0000ff;\">=<\/span><span style=\"color: #000000;\">[System.Windows.Forms.TreeViewCancelEventHandler]{\r\n<\/span><span style=\"color: #008000;\">#Event Argument: $_ = [System.Windows.Forms.TreeViewCancelEventArgs]<\/span>\r\n    <span style=\"color: #0000ff;\">if<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #8b0000;\">$_<\/span><span style=\"color: #000000;\">.Node.Checked)\r\n    { <\/span><span style=\"color: #008000;\"># Prevent node from unchecking<\/span>\r\n        <span style=\"color: #8b0000;\">$_<\/span><span style=\"color: #000000;\">.Cancel <\/span><span style=\"color: #0000ff;\">=<\/span> <span style=\"color: #8b0000;\">$true<\/span><span style=\"color: #000000;\">\r\n    }\r\n}\r\n<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong>BeforeCollapse<\/strong><\/p>\n<p>This event occurs before the tree node is collapsed.<\/p>\n<p>Use this event to cancel the action.<\/p>\n<p><strong>BeforeExpand<\/strong><\/p>\n<p>This event occurs before the tree node is expanded.<\/p>\n<p>Use this event to cancel the action.<\/p>\n<p><strong>BeforeLabelEdit<\/strong><\/p>\n<p>This event occurs before the tree node label text is edited. Label editing must be enabled for this event to be called (See LabelEdit property).<\/p>\n<p>This event uses the [System.Windows.Forms.NodeLabelEditEventArgs] argument. See AfterLabelEdit for more information about this argument.<\/p>\n<p>Use this event to prevent users from renaming specific nodes.<\/p>\n<p><strong>BeforeSelect<\/strong><\/p>\n<p>This event occurs before the tree node is selected.<\/p>\n<p>You can use this event to prevent specific nodes from being selected.<\/p>\n<p><em><strong>Click Events:<\/strong><\/em><\/p>\n<p>The \u2018Click\u2019 events have the following as an argument that is accessible via the <span style=\"color: #8b0000;\">$_<\/span> variable:<\/p>\n<p><strong>[System.Windows.Forms.TreeNodeMouseClickEventArgs]<\/strong><\/p>\n<table border=\"1\" width=\"450\" cellspacing=\"0\" cellpadding=\"2\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"80\"><strong>Properties<\/strong><\/td>\n<td valign=\"top\" width=\"370\"><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"80\">Button<\/td>\n<td valign=\"top\" width=\"370\">Gets which mouse button was pressed.<\/p>\n<p>Values:<\/p>\n<p>Left<br \/>\nThe left mouse button was pressed.<\/p>\n<p>None<br \/>\nNo mouse button was pressed.<\/p>\n<p>Right<br \/>\nThe right mouse button was pressed.<\/p>\n<p>Middle<br \/>\nThe middle mouse button was pressed.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"80\">Clicks<\/td>\n<td valign=\"top\" width=\"370\">Gets the number of times the mouse button was pressed and released.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"80\">Location<\/td>\n<td valign=\"top\" width=\"370\">Gets the location of the mouse during the generating mouse event.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"80\">Node<\/td>\n<td valign=\"top\" width=\"370\">Gets the node that was clicked.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p><strong>NodeMouseClick<\/strong><\/p>\n<p>This event occurs when the user clicks a TreeNode with the mouse.<\/p>\n<p>Example:<\/p>\n<pre><span style=\"color: #8b0000;\">$treeview1_NodeMouseClick<\/span><span style=\"color: #0000ff;\">=<\/span><span style=\"color: #000000;\">[System.Windows.Forms.TreeNodeMouseClickEventHandler]{\r\n<\/span><span style=\"color: #008000;\">#Event Argument: $_ = [System.Windows.Forms.TreeNodeMouseClickEventArgs]<\/span>\r\n    <span style=\"color: #0000ff;\">if<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #8b0000;\">$_<\/span><span style=\"color: #000000;\">.Button <\/span><span style=\"color: #0000ff;\">-eq<\/span> <span style=\"color: #ff0000;\">'Right'<\/span><span style=\"color: #000000;\">)\r\n    {\r\n        <\/span><span style=\"color: #0000ff; font-weight: bold;\">Write-Host<\/span> <span style=\"color: #ff0000;\">\"Right Click Node: \"<\/span> <span style=\"color: #8b0000;\">$_<\/span><span style=\"color: #000000;\">.Node.Text\r\n        <\/span><span style=\"color: #8b0000;\">$this<\/span><span style=\"color: #000000;\">.SelectedNode <\/span><span style=\"color: #0000ff;\">=<\/span> <span style=\"color: #8b0000;\">$_<\/span><span style=\"color: #000000;\">.Node <\/span><span style=\"color: #008000;\">#Select the node (Helpful when using a ContextMenuStrip)<\/span><span style=\"color: #000000;\">\r\n    }\r\n}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong>NodeMouseDoubleClick<\/strong><\/p>\n<p>Occurs when the user double-clicks a TreeNode with the mouse.<\/p>\n<pre><span style=\"color: #8b0000;\">$treeview1_NodeMouseDoubleClick<\/span><span style=\"color: #0000ff;\">=<\/span><span style=\"color: #000000;\">[System.Windows.Forms.TreeNodeMouseClickEventHandler]{\r\n<\/span><span style=\"color: #008000;\">#Event Argument: $_ = [System.Windows.Forms.TreeNodeMouseClickEventArgs]<\/span>\r\n    <span style=\"color: #0000ff; font-weight: bold;\">Write-Host<\/span> <span style=\"color: #ff0000;\">\"Double Click \"<\/span> <span style=\"color: #8b0000;\"><span style=\"color: #8b0000;\">$_<\/span><span style=\"color: #000000;\">.Node.Text<\/span><\/span><span style=\"color: #000000;\">\r\n}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Next:<\/strong> <a href=\"http:\/\/www.sapien.com\/blog\/2011\/11\/23\/primalforms-2011-spotlight-on-the-treeview-control-part-2\/\">Spotlight on the TreeView Control &#8211; Part 2<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Spotlight on Controls series describes the controls, that is, the objects in the System.Windows.Forms namespace, so you can use them effectively when building GUI apps in PowerShell Studio and PrimalScript. Each post focuses on one control and lists its most important properties, methods, and events, including the default event that PowerShell Studio adds to [&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-3544","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\/3544","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=3544"}],"version-history":[{"count":12,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3544\/revisions"}],"predecessor-version":[{"id":11057,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3544\/revisions\/11057"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=3544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=3544"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=3544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}