如何检查treeview节点的名称是否与c#中的任何其他节点相同 [英] how to check name of treeview nodes is same or not any other nodes in c#
本文介绍了如何检查treeview节点的名称是否与c#中的任何其他节点相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我使用rename();
但任何节点名称都与新名称相同
则名称不会更改
if i use rename();
but any node name is same of new name
then name is not change
推荐答案
我明白了:)这是你的解决方案。
I understand :) here is your solution.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Test_TreeView
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load += Form1_Load;
}
void Form1_Load(object sender, EventArgs e)
{
this.RenameNode("Node7", "Node6");
}
void RenameNode(string oldt, string newt)
{
// if new text exists return
if (CheckNodeText(this.treeView1.TopNode, newt))
return;
// if node found, rename it
TreeNode n = FindNode(this.treeView1.TopNode, oldt);
if (n != null)
n.Text = newt;
}
// check text if exists
bool CheckNodeText(TreeNode node, string text)
{
if (node.Text == text)
return true;
for (int i = 0; i < node.Nodes.Count; i++)
{
if (CheckNodeText(node.Nodes[i], text))
return true;
}
return false;
}
// find the node having text
TreeNode FindNode(TreeNode node, string text)
{
if (node.Text == text)
return node;
for (int i = 0; i < node.Nodes.Count; i++)
{
TreeNode n = FindNode(node.Nodes[i], text);
if (n != null)
return n;
}
return null;
}
}
}
这篇关于如何检查treeview节点的名称是否与c#中的任何其他节点相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文