这篇文章主要介绍了C#搜索TreeView子节点,保留父节点的方法,实例分析了C#操作TreeView节点的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了C#搜索TreeView子节点,保留父节点的方法。分享给大家供大家参考。具体如下:
C# 搜索TreeView子节点保留父节点:
- private void selectNodeByName(string name)
- {
- //重新生生成树,
- InitTvCatalog();
- //删除节点
- selectNode(tvCatalog.Nodes, name);
- }
- private void selectNode(TreeNodeCollection nodes, string name)
- {
- bool del = true;
- for (int i = nodes.Count - 1; i >= 0; i--)
- {
- TreeNode node = nodes[i];
- del = true;
- if (node.Nodes.Count != 0)
- {
- selectNode(node.Nodes, name);
- }
- if (node.Text.StartsWith(name, StringComparison.OrdinalIgnoreCase))
- {
- del = false;
- }
- else
- {
- del = true;
- }
- if (del && node.Nodes.Count == 0)
- {
- node.Remove();
- }
- }
- tvCatalog.ExpandAll();
- }
WPF搜索TreeView子节点保留父节点:
- private void selectNodeByName(string name)
- {
- //重新生生成树,
- method = new Common.PublicMethod();
- method.TreeViewLoad();
- //删除节点
- selectNode(RegimeSetting.Common.StaticVariables.treeView.Items, name);
- method = null;
- }
- private void selectNode(ItemCollection nodes, string name)
- {
- bool del = true;
- for (int i = nodes.Count - 1; i >= 0; i--)
- {
- TreeViewItem node = nodes[i] as TreeViewItem;
- del = true;
- if (node.Items.Count != 0)
- {
- selectNode(node.Items, name);
- }
- if (node.Header.ToString().StartsWith(name, StringComparison.OrdinalIgnoreCase))
- {
- del = false;
- }
- else
- {
- del = true;
- }
- if (del && node.Items.Count == 0)
- {
- if ((node.Parent as TreeViewItem) != null)
- (node.Parent as TreeViewItem).Items.Remove(node);
- else
- (node.Parent as TreeView).Items.Remove(node);
- }
- }
- (RegimeSetting.Common.StaticVariables.treeView.Items[0] as TreeViewItem).IsExpanded = true;
- }
希望本文所述对大家的C#程序设计有所帮助。
新闻热点
疑难解答