views:

701

answers:

1

Hello,

I have a treeview control in WinForms that is filled from a wide variety of database content. I need to calculate the minimum width of the treeview control (after it is populated) so that horizontal scroll bars are not necessary to view the content. The calculation needs to account for the size of the longest text in the tree but it also needs to account for indent level, hierarchy graphics, etc.

I don't see an obvious way to horizontally autosize the treeview control based on its content. Has anyone found a good way to accomplish this?

Thanks in advance...

Mark

+1  A: 

Try scanning through the list, computing the maximum of:

text_width + indent_size * indent_level + image_width

a single iteration through the list should result in a fairly good estimate on the width of the treeview. You might want to iterate all nodes or just visible nodes, depending on your application.

Jim H.