How to let the TreeView
to change its width when expanding node so that the label of node will completely show.
First I set the DrawMode = OwnerDrawAll;
Then handle the event DrawNode
andin handler
e.DrawDefault = true;
currentWith_ = Math.Max(currentWith_, e.Node.Bounds.Right);
and then in AfterExpand
sets the control with. But is not works every time. Sometimes the with is not changed or changed not correctly.
How to correct this problem. Thanks in advance.
Try this one, this 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);
}