Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a tree view control which has some nodes. I want Auto Ellipsis property to tree view node as like label control have. I did Google but not get relevant information or method to achieve this task.
I am using c# as a programing language and .net framework 3.5 as a platform. I am making program in windows form not in WPF.
Thanks.
Posted
Updated 7-Jan-12 0:54am
v2

1 solution

In the standard WinForms TreeView Control achieving this is going to be complex, and I suspect, that you will not be satisfied with performance, or results when the TreeView contains a large number of nodes.

Yes, I have implemented something similar with the WinForms TreeView, and don't use it anymore: it was a pain-in-the-rear to use and maintain, and performance with large number of Nodes was not satisfactory.

Lack of facilities, like what you describe here, are what motivated me, years ago, to research 3rd. party TreeView components, and I ended up purchasing the IntegralUI TreeView from Lidor Systems, which I highly recommend, and which has evolved remarkably in every way over the last few years, and is accompanied by first-class highly-responsive technical-support.

Although the feature I just mentioned was: one of the less important reasons I chose the remarkable IntegralUI TreeView, whose functionality, imho, makes the WinForms TreeView seem like what it is: a hard-to-use, very limited performance, "toy" which is a .NET wrapper around a "COM Antique." And, no I do not work for Lidor ! :)

Here's some "facts" to consider: re standard WinForms TreeView

1. You cannot add Controls to a Node (in the IntegralUI TV you can).

2. You can add a Control, like a Label, to the TreeView.Controls ControlCollection itself, and yes, you can move it around based on where the Mouse is, or is moving, or is hovering ... with some important limitations. The advantage of adding a Label directly to the TreeView: Label co-ordinate space is in TreeView co-ordinate space. Disadvantage: the Label width is going to be determined by the TreeView width.

3. The simplest idea, perhaps, is adding a Label with transparent background to the Form itself, and then doing the right thing to use it as a pop-up over the Node whose Text has the Ellipsis: that will involve some complex transformations from Node Bounding Box space (which is in TreeView co-ordinates) to Label Location which is in Form co-ordinates. Advantage: Label width can be as long as you need. Disadvantage: complex co-ordinate transforms, as just mentioned.

4. Then, consider you have a very large number of Nodes: for everyone of those Nodes you will need to deal with the variable size of the Text entry visible in the Node: you will need to "trim" the text, and add the Ellipsis at the right place ... depending on the width of the Node bounding box: the extent of the available width for the Text ... is going to vary as the left margin gets larger for each level of nesting.

... And now think about having to do that all over again if the TreeView is resized !

... And now think about the TreeView supporting horizontal scrolling ! Are you going to "grow" your visible portion of Node Text and adjust Ellipsis dynamically with every horizontal scroll: imho: if you are sane you will not :)

To achieve any of the above you are going to either have to keep a "flattened" list of all Nodes, or recursively parse the whole TreeView every time you need to adjust the Text displayed in all Nodes (with IntegralUI TreeView a flattened, always current, NodeList is a provided feature).

5. Now consider you want to "simulate" the Label Control's Ellipsis auto-expand behavior in other ways:

a. you could try using a ToolTip: you will fail to find that satisfactory, believe me, and the idea of using one ToolTip per Node : please: don't even imagine it :)

b. or, going back to the idea you added a Label directly to the TreeView (see #2 above).

1. you can try handling the NodeMouseHover Event for the TreeView, and calculating the position of the Node the Mouse is over, then try moving the Label so it fits just over the normally displayed Text in the Node ... and then ...

a. You are going to find using the Hover Event consistently frustrating, believe me.

b. You will need to do something if the Label ... now over the Node ... is clicked to simulate the Node being clicked: that's not hard to do.

In conclusion, it's my humble opinion that simulating the very nice Ellipsis auto-expand feature of a Label in a WinForms TreeView is not a good idea: the costs far outweigh any small GUI/UX benefits.
 
Share this answer
 
v3

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