开发者

Is it possible to make a node invisible in a Winforms TreeView?

开发者 https://www.devze.com 2022-12-13 02:23 出处:网络
I was thinking that that this would be a useful way to display what would appear to be a Treeview with multiple top nodes.开发者_C百科

I was thinking that that this would be a useful way to display what would appear to be a Treeview with multiple top nodes.开发者_C百科

Example: Lets say that I want to create a TreeView of geography and I want the top nodes(s) to be countries. I could do this by:

  • World
    • Mexico
    • Japan

But what the user wants is:

  • Mexico
  • Japan

It would be nice if I could build one TreeView and simply hide World as opposed to building two TreeViews.

Update==========================

The reason that I wanted to do this was that I have routines that process the TreeView recursively, for example walking the tree before an update to preserve its collapse/expand state. It would be nice to have an invisible topnode to start processing at.


No; and if you think about it, wouldn't you expect all children to be hidden if the parent node was hidden?

What I would do is maintain the list outside of the view, and copy in items starting at a specified node.


Urm you can add multiple nodes at the root level. i.e. adding something like

treeView1.Nodes.Add("Hello 1").Nodes.Add("Hello 3");
treeView1.Nodes.Add("Hello 2").Nodes.Add("Hello 4");
does just that for me at least.

EDIT: If you really must have a root node for traversing the tree (and imo you are just lazy for not have 1 more function) then you can use the following code, at least for .NET 2 (not checked on anything else). Note that I DO NOT RECOMMEND THIS but hey you asked for it ;)

TreeNode realRoot = (TreeNode)treeView1.GetType().InvokeMember("root", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance, null, treeView1, null);

The child nodes of the realRoot will still return null for their parent (that is how the code is designed) but if you must have it for forward use then that will work :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号