English 中文(简体)
只显示被搜索的树木
原标题:Displaying only searched treenode

我在寻找 no子时,在窗户应用上有树木观,我需要隐藏所有剩余的 no子,我只需要显示被搜索的 no子及其母子。 喜欢

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.

最佳回答

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

藏匿点的唯一途径是将他们从诺琴斯收集中除去。

这意味着要再次显示它们,就要求你在树木等级内跟踪其位置,以便能够恢复这些位置。

下面的法典似乎符合你的要求:

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; }
    }
}

希望有助于你们。

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.

详情见。 如何接受回答工作?

问题回答

暂无回答




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签