Best way to horizontally autodetect a TreeView WinForms control?

I have a TreeView control in WinForms that populates from a wide variety of database contents. I need to calculate the minimum width of a TreeView control (after filling) so that horizontal scrollbars are not needed to view the content. The calculation should take into account the size of the longest text in the tree, but it should also take into account the level of indentation, hierarchical graphics, etc.

I see no obvious way to horizontally authorize a treeview control based on its contents. Has anyone found a good way to do this?

Thanks in advance...

Mark

+3
source share
1 answer

Try scanning through the list, calculating the maximum:

text_width + indent_size * indent_level + image_width

one iteration over the list should lead to a pretty good estimate of the width of the tree. You might want to iterate over all nodes or only visible nodes depending on your application.

+1
source

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


All Articles