将节点添加到树视图 [英] add node to treeview

查看:99
本文介绍了将节点添加到树视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此代码将节点从文本框添加到树视图中,如果此节点等于在树视图中找到的节点,则新节点必须是下一个旧节点,并且当我单击按钮时颜色为黄色问题什么都没发生

Hi, I'm trying in this code to add node from textbox to treeview if this node equal to node that is found in treeview the new node must be next old node and have color yellow the problem when I click to the button nothing happen

for (int i = 0; i < treeView1.Nodes.Count; i++)
           {
               string p = treeView1.Nodes[i].Text;
               if (p == textBox1.Text)
               {
                   treeView1.SelectedNode.NextNode.Nodes.Add(textBox1.Text);
                   treeView1.SelectedNode.BackColor = Color.Yellow;

               }
           }

推荐答案

而不是使用:



string p = treeView1.Nodes [i] .ToString();



使用:



string p = treeView1.Nodes [i] .Text;



在'TreeNode上调用'ToString将返回类似于:



TreeNode :Node0



我建议你修改你的问题并澄清:



0.你说你有子节点,但代码中的'for循环只枚举根级节点:这是你的意图吗?



1。你想在哪里插入新的TreeNode?



2.为什么你想要两个具有相同可见'Text属性的TreeNode?



3.这里'SelectedNode的作用是什么,为什么要更改'BackColor?
Instead of using:

string p = treeView1.Nodes[i].ToString();

use:

string p = treeView1.Nodes[i].Text;

Calling 'ToString on a 'TreeNode will return something like:

"TreeNode: Node0"

I suggest you revise your question and clarify:

0. you say you have sub-nodes, but the 'for loop in your code only enumerates the root-level nodes: is that your intent ?

1. where exactly do you want to insert the new TreeNode ?

2. why would you want to have two TreeNodes with the same visible 'Text property ?

3. what is the role of the 'SelectedNode here, and why do you changes its 'BackColor ?


这篇关于将节点添加到树视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆