如何在最后一个树视图中添加Treenode [英] How Do I Add A Treenode At The Last Of Tree View

查看:129
本文介绍了如何在最后一个树视图中添加Treenode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我通过从调用方法中获取每个节点,在方法中添加treeview节点。但是在循环中,我得到一个文本为其他的节点。



我想将此节点添加到树视图中,但它应显示为最后一个后代。 br $> b $ b

如何实现这一目标?任何帮助

解决方案

Yash35写道:

 m_oTree.Nodes.Find(oNode.ParentKey,true)[0] .Nodes.Add( oNode.Key,oNode.DisplayText,ReturnImageListIndex(oNode),ReturnImageListIndex(oNode)); 

我正在使用上面的代码行向treeview添加节点。
它是Windows窗体。我希望它是其父节点的最后一个后代,即根节点。
我的树有一个根节点

在WinForm中,Microsoft TreeView没有TreeNode的.ParentKey属性,someNode.Parent.Key也是一个错误。



虽然有可能你有分类TreeNode,并添加了'ParentKey字段......我对此表示怀疑。



如果您的设计中有一个根节点,那么为什么不保留指向根节点的节点集合的指针;那么,你可以肯定,如果你使用'Add on the collection,你添加的TreeNode将是TreeView中的最后一个TreeNode

  //  在表单范围中创建变量 
private TreeNodeCollection rootNodes;

// 在应用程序代码的方法中:
// 在创建根节点的地方,
// 例如:在表单中加载EventHandler
private void YourForm_Load( object sender,EventArgs e)
{
rootNodes = yourTreeView.Nodes;

// 其他......
}

// 使用'rootNodes的示例:
//
private < span class =code-keyword> void someAddTreeNodeMethod( string nodeText)
{
rootNodes.Add( new TreeNode(nodeText));
// 做正确的事情来设置它的ImageList指针等。
}


Hi,

I am adding nodes for treeview in a method by getting each node from a calling method. But while in loop i get a node whose text is "Other".

I want to add this node as well to treeview but it should display as last descendant.

How to i achieve this? Any help

解决方案

Yash35 wrote:

m_oTree.Nodes.Find(oNode.ParentKey, true)[0].Nodes.Add(oNode.Key, oNode.DisplayText, ReturnImageListIndex(oNode), ReturnImageListIndex(oNode));
 
i am adding nodes to treeview with above line of code.
it is windows forms. I want it to be last descendant of its parent node i.e root node.
My tree has a single root node

In the WinForm Microsoft TreeView there is no .ParentKey property of a TreeNode, and someNode.Parent.Key would be an error, also.

While it is possible you have sub-classed TreeNode, and added a 'ParentKey field ... I doubt that.

If you have a single root node in your design, then why not keep a pointer to the Nodes Collection of the root node; then, you can be certain that if you use 'Add on that collection, the TreeNode you add will be the last TreeNode in the TreeView

// create a variable in Form scope
private TreeNodeCollection rootNodes;

// in a method in your application code:
// at a place where the root-node has been created,
// for example: in the Form Load EventHandler
private void YourForm_Load(object sender, EventArgs e)
{
    rootNodes = yourTreeView.Nodes;
    
    // whatever else ...
}

// example of using 'rootNodes:
// in some method:
private void someAddTreeNodeMethod(string nodeText)
{
   rootNodes.Add(new TreeNode(nodeText));
   // do the right thing to set its ImageList pointer, etc.
}


这篇关于如何在最后一个树视图中添加Treenode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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