thiagoprzy
thiagoprzy

Reputation: 421

How can I get the full size of a Tree View in Windows Forms?

I don't have the "AutoSize" option in TreeView, but I need to know what's the full height and the full width that the object is taking. I can only define the size of the TreeView panel, which shows scrollbar when the content overlaps the referred size.

Is there a way to know how big really is the content displayed?

Thank you

Upvotes: 0

Views: 4281

Answers (1)

SPFiredrake
SPFiredrake

Reputation: 3892

If you want to know the absolute bottom of the content area (only what's expanded), then you can use the Nodes property with the Bounds property to get the visible height.

TreeNode tn = tv.Nodes[tv.Nodes.Count - 1];
while(tn.IsExpanded)
    tn = tn.Nodes[tn.Nodes.Count - 1];
return tn.Bounds.Bottom;

Just be sure to have proper error checking (TreeView actually HAS nodes, etc). As for the width, can't remember exactly how I did it. However, you might be able to use the TreeView's Bounds property itself (might require some testing). I had a similar situation, where I didn't have an AutoSizing TreeView, but it was contained in a panel and fill docked, so I needed to handle the scrollbars myself by resizing the treeview on Expand/Collapse.

Upvotes: 4

Related Questions