添加子节点 C# 的问题
trouble with adding child node C#
我正在将一个子节点添加到树视图中的当前父节点。但我的问题是它将新节点添加到当前父节点的末尾,而不是添加到 if
为真的位置。
这是我的代码:
for (int i = 0; i < num; i++)
{
if (action_type1 != action_type2)
{
TreeNode new_node = = treeView1.Nodes[0].Nodes[position];
string new_name = "";
new_node.Nodes.Add(new_name);
}
}
当然 num
、position
、action_type1
和 action_type2
是我代码中的变量,对于任何 for 循环,它们是不同的整数和字符串。 action_type1
是treeView的节点名,action_type2
是一个固定的字符串。 if
循环查找整个树,如果有节点与给定字符串相等,则离开该节点,否则在树中插入一个空节点,然后递归执行。
但为简单起见,我们有:
int num = 2;
int position = 4;
string action_type1;
string action_type2;
这就是你想要的?
for (int i = 0; i < num; i++)
{
if (action_type1 != action_type2)
{
treeView1.Nodes[i].Nodes.Insert(position - 1, virtual_name);
}
}
我正在将一个子节点添加到树视图中的当前父节点。但我的问题是它将新节点添加到当前父节点的末尾,而不是添加到 if
为真的位置。
这是我的代码:
for (int i = 0; i < num; i++)
{
if (action_type1 != action_type2)
{
TreeNode new_node = = treeView1.Nodes[0].Nodes[position];
string new_name = "";
new_node.Nodes.Add(new_name);
}
}
当然 num
、position
、action_type1
和 action_type2
是我代码中的变量,对于任何 for 循环,它们是不同的整数和字符串。 action_type1
是treeView的节点名,action_type2
是一个固定的字符串。 if
循环查找整个树,如果有节点与给定字符串相等,则离开该节点,否则在树中插入一个空节点,然后递归执行。
但为简单起见,我们有:
int num = 2;
int position = 4;
string action_type1;
string action_type2;
这就是你想要的?
for (int i = 0; i < num; i++)
{
if (action_type1 != action_type2)
{
treeView1.Nodes[i].Nodes.Insert(position - 1, virtual_name);
}
}