Node partially disabled in winform TreeView

I am having trouble figuring out what the problem is. I googled around and did not find many solutions to this problem. The only β€œsolution” I found was a hack to expand and then collapse the last node.

this.Nodes[this.Nodes.Count - 1].Expand(); this.Nodes[this.Nodes.Count - 1].Collapse(); 

As you can see in this screenshot, the last node is partially disabled, and the only way to expose it is to expand the node, which will make the TreeView correctly display it.

enter image description here

I pragmatically add nodes to the TreeView. I don’t know if this affects the result, but I expanded TreeView to my own class, so I can add several properties and methods to it.

 public class MyTreeView : TreeView { public void BuildTree() { this.Nodes.Clear(); foreach (TestSetFolder folder in Folders) { MyTreeNode node = new MyTreeNode(); node.Name = folder.Name; node.Text = folder.Name; node.Tag = folder; node.FolderID = folder.NodeID; node.IsPopulated = false; this.Nodes.Add(node); } } } 

This is how I add nodes to the list. Does anyone have a clean solution to this problem?

+4
source share
3 answers

Use treeView.BeginUpdate() and treeView.EndUpdate() before and after any visual changes.

SuspendLayout () and ResumeLayout () can also be useful!

If you want to update the user interface, do not add all nodes at once! add one after the other, sandwiched between begin and endupdate calls.

+6
source

You can call EnsureVisible on the TreeViewNode in question, for example:

 treeView1.Nodes[treeView1.Nodes.Count - 1].EnsureVisible(); 

Refer to the MSDN entry for more information.

Edit:
I think I found it. You probably have a root node, and the node you want to scroll through is a sub-root of this root node. Try instead:

  TreeNode rootNode = treeView1.Nodes[0]; TreeNode lastNode = rootNode.Nodes[rootNode.Nodes.Count - 1]; lastNode.EnsureVisible(); 

Or use the example from the MSDN article to get the last node:

 TreeNode lastNode = treeView1.Nodes[treeView1.Nodes.Count - 1]. Nodes[treeView1.Nodes[treeView1.Nodes.Count - 1].Nodes.Count - 1]; 
+1
source

Look at the Expand method in the TreeNode class. See http://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.aspx

0
source

Source: https://habr.com/ru/post/1432723/


All Articles