Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6362303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:53:27+00:00 2026-05-24T23:53:27+00:00

I have treeview in windowsform application when searching for node is performed I need

  • 0

I have treeview in windowsform application when searching for node is performed I need to hide all the remaining node and I need to show only the searched node and its parent .like

grandParent
Parent1
child1
child2
child3.
parent2
child4
child5
if the searched node is child 3 i need to show the out put as..

grandParent
Parent1
child3
all auother are to be Hide.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:53:28+00:00Added an answer on May 24, 2026 at 11:53 pm

    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).

    The only way of hiding nodes is to remove them from the Nodes collection.

    This means displaying them again would require you to keep track of their location within the tree hierarchy to be able to restore them.

    The following code seems to do what you require:

    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<RemovedTreeNode> _removedNodes = new List<RemovedTreeNode>();
    
            public HideRestoreNodesForm()
            {
                InitializeComponent();
    
                //AddNodesToTree();
            }
    
            private void searchButton_Click(object sender, EventArgs e)
            {
                TreeNode[] foundNodes = treeView1.Nodes.Find("NameOfNodeToFind", true);
                if(foundNodes.Length > 0)
                {
                    TreeNode foundNode = foundNodes[0];
                    HideNodes(treeView1.Nodes, foundNode);
                }
            }
    
            private void HideNodes(TreeNodeCollection nodes, TreeNode visibleNode)
            {
                List<TreeNode> nodesToRemove = new List<TreeNode>();
                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) && !IsNodeAncestor(secondNode, firstNode) && 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; }
        }
    }
    

    Hope this helps you.

    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.

    See the following for more information: How does accepting an answer work?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a TreeView control in my WinForms .NET application that has multiple levels
Using WPF, I have a TreeView control that I want to set its ItemTemplate
I have a WinForms TreeView with one main node and several sub-nodes. How can
I have treeview and 2 imagelist first image list contains images only 42x42 and
I have a treeview control in my web application. I build this tree view
I have a treeview control in a Windows Forms project that has checkboxes turned
I have a treeview with nodes like this: Foo (1234), and want to allow
I have a TreeView windows forms control with an ImageList , and I want
I have a treeview control on an aspx page. The data comes from database
I have a treeview in my masterpage. When a contentpage is loaded i want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.