我在寻找 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.
我在寻找 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.
详情见。 如何接受回答工作?
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 ...
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 ...
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. ...
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#?
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 ...
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 ...
When I m trying to change the default Image of a Control on Windows Forms in Form Designer (no matter where on which control) I get this error: Error message: An item with the same key has ...
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.