C#搜索TreeView子节点,保留父节点的方法
时间:2020-10-27 15:41:51|栏目:.NET代码|点击: 次
本文实例讲述了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#程序设计有所帮助。
栏 目:.NET代码
本文地址:http://www.codeinn.net/misctech/16421.html