Click here to Skip to main content
15,882,063 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
I am developing my own "treeview with column" user control in c# but if the text content of tree view node is larger than width of my column then text content occupy the space of another column and it looks ugly so how i can hide that text within width of list view.
Posted
Updated 3-Sep-15 6:02am
v2
Comments
Kevin Marois 3-Sep-15 12:13pm    
Are you using WinForms, WPF, Web?
vishal2592 3-Sep-15 12:34pm    
Winforms

1 solution

Just one quick and dirty work-around method is to assign some string values to each node. They won't be visible, because you use owner draw, but will define rendered node bounds.

No matter how you control the width, there is one difficult little problem: the measurement of the string width is never accurate. I saw several solutions for System.Drawing used with System.Windows.Forms, but those solution still did not give me required accuracy, so the text still did not fit exactly (it would not be a problem for monospace fonts, of course). So, I had to reserve good amount of extra space for the node, which would look really ugly if the background color of the note was different from the background color of the tree view.

Overall, Windows tree view used in System.Windows.Forms looks obsolete and pretty ugly for complicated highly customized controls; one of the problems was fighting with the flicker. It feels like it was all designed when the video cards were really slow.

That brings us to another alternative I used. Ultimately, I decided to draw the whole tree in a custom way, by overriding TreeView.OnPaint. The base class still preserves bounds of each node, so you can use these bounds for your rendering, but then you can just ignore the right bound of tree node bounds. This solution works better for me, as the measurement of the individual node widths went out of equation. I completely eliminated flicker through the use of double buffering. (I must admit my rendering was highly customized, with complicated updates to the view on each change of selection and semantic operations. For simpler applications, the flicker may not appear.)

Another alternative I would probably considered first these days would be moving to WPF. In WPF, you can fully customize representation of each node.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900