//实例代码:
/// <summary>
/// 添加节点事件
/// </summary>
/// <param name="tnodes"></param>
/// <param name="nodeName">父节点名称</param>
/// <param name="subNodeName">添加的节点名称</param>
/// <param name="key"></param>
/// <returns></returns>
private string AddNodes(TreeNodeCollection tnodes, string nodeName, string subNodeName, string key = "")
{
string Msg = string.Empty;
foreach (TreeNode node in tnodes)
{
if (node.Text == nodeName)
{
int tag = -1;
GetNodes(node.Nodes, subNodeName, out tag);
if (tag == 1)
{
//给指定的节点增加子节点
node.Nodes.Add(key, subNodeName);
Msg = "添加成功";
break;
}
else
{
Msg = "该节点下已存在此子节点";
}
}
//ShowNodes(node.Nodes,nodeName,subNodeName);
}
return Msg;
}
/// <summary>
/// 查找指定节点是否包含某个子节点
/// </summary>
/// <param name="tnodes"></param>
/// <param name="nodeName"></param>
/// <param name="tag">有返回0 无返回1</param>
/// <returns></returns>
private TreeNode GetNodes(TreeNodeCollection tnodes, string nodeName, out int tag)
{
foreach (TreeNode node in tnodes)
{
if (node.Text == nodeName)
{
tag = 0;
return node;
}
}
tag = 1;
return new TreeNode();
}