How to calculate and change the width of a tree

How to allow TreeView to change width when expanding the node so that the node label is fully displayed.

First I set DrawMode = OwnerDrawAll;

Then handle the DrawNode event and the handler

 e.DrawDefault = true; currentWith_ = Math.Max(currentWith_, e.Node.Bounds.Right); 

and then in AfterExpand installs the control with. But this does not work every time. Sometimes c does not change or changes incorrectly.

How to fix this problem. Thanks in advance.

+3
source share
2 answers

Try this, it works successfully:

 private void treeViewAfterExpand(object sender, TreeViewEventArgs e) { int maxRight = treeView.ClientSize.Width; if(e.Node.Nodes != null) foreach (TreeNode node in e.Node.Nodes) { maxRight = Math.Max(maxRight, node.Bounds.Right); } treeView.ClientSize = new Size(maxRight, treeView.ClientSize.Height); } 
+5
source

The solution given by Ria works, but not when expanded in the constructor. Extending the load event instead of the constructor made it work. (I can not comment, because below 50 points.)

+1
source

All Articles