Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Unfortunately (as far as I know) if you are using a WinForms TreeView control then hiding nodes is not as simple as setting the IsVisible property (due to the fact that the property is read only).</p> <p>The only way of hiding nodes is to remove them from the Nodes collection.</p> <p>This means displaying them again would require you to keep track of their location within the tree hierarchy to be able to restore them.</p> <p>The following code seems to do what you require:</p> <pre><code>using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using ClassLibrary; using System.Xml; using System.Diagnostics; using System.IO; using System.Xml.Linq; namespace WindowsFormsApplication { public partial class HideRestoreNodesForm : Form { private List&lt;RemovedTreeNode&gt; _removedNodes = new List&lt;RemovedTreeNode&gt;(); public HideRestoreNodesForm() { InitializeComponent(); //AddNodesToTree(); } private void searchButton_Click(object sender, EventArgs e) { TreeNode[] foundNodes = treeView1.Nodes.Find("NameOfNodeToFind", true); if(foundNodes.Length &gt; 0) { TreeNode foundNode = foundNodes[0]; HideNodes(treeView1.Nodes, foundNode); } } private void HideNodes(TreeNodeCollection nodes, TreeNode visibleNode) { List&lt;TreeNode&gt; nodesToRemove = new List&lt;TreeNode&gt;(); foreach (TreeNode node in nodes) { if (!AreNodesRelated(node, visibleNode)) { _removedNodes.Add(new RemovedTreeNode() { RemovedNode = node, ParentNode = node.Parent, RemovedNodeIndex = node.Index }); nodesToRemove.Add(node); } else { HideNodes(node.Nodes, visibleNode); } } foreach (TreeNode node in nodesToRemove) node.Remove(); } private bool AreNodesRelated(TreeNode firstNode, TreeNode secondNode) { if (!IsNodeAncestor(firstNode, secondNode) &amp;&amp; !IsNodeAncestor(secondNode, firstNode) &amp;&amp; firstNode != secondNode) { return false; } else { return true; } } private bool IsNodeAncestor(TreeNode nodeToCheck, TreeNode descendantNode) { TreeNode parentNode = descendantNode.Parent; while (parentNode != null) { if (parentNode == nodeToCheck) { return true; } else { parentNode = parentNode.Parent; } } return false; } private void restoreNodes_Click(object sender, EventArgs e) { RestoreNodes(); } private void RestoreNodes() { _removedNodes.Reverse(); foreach (RemovedTreeNode removedNode in _removedNodes) { if (removedNode.ParentNode == null) treeView1.Nodes.Add(removedNode.RemovedNode); else removedNode.ParentNode.Nodes.Insert(removedNode.RemovedNodeIndex ,removedNode.RemovedNode); } _removedNodes.Clear(); } } public class RemovedTreeNode { public TreeNode RemovedNode { get; set; } public int RemovedNodeIndex { get; set; } public TreeNode ParentNode { get; set; } } } </code></pre> <p>Hope this helps you.</p> <p>I notice you are a new user, If this or any other questions you ask on the site provide the answers you are looking for remember to accept the answers.</p> <p>See the following for more information: <a href="https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work#5235">How does accepting an answer work?</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload