Treeview Bug

It is not possible to make a single node bold without the end of the node text being clipped.  The work around this problem, I tried setting all nodes to bold and then setting all but one to regular.  The problem now is that the selection box when selecting a non-bold node is too big!

This is quite a major problem as it means .NET developers cannot replicate the look and feel of the standard Windows treeview.

Is this a known issue and if so, when will it be fixed


Answer this question

Treeview Bug

  • Lei Jiang

    Hi,

    For me the above code worked fine. 
    But i m facing the problem for the TreeNode Control.

    For the TreeNode the the text of the node who are shown in bold color still shows me the Text Clipped.

    Any Help in that direction

    Thanks
    Vikash

  • Ken Grissom

    I have also been struggling with this problem for over a month.  I have been developing a commercial application for our company that relies on a tree view that can display particular nodes using a bold font, similar to Outlook, similar to the Solution Explorer in Visual Studio .NET.  I've spent many, many hours struggling with this issue going so far as to delve into the murky waters of API calls, Windows messages, and CustomDraw.  I nearly resorted to drawing the entire control myself or scrapping the whole thing and finding a third party control.

    I've discovered that many other people have run into this frustrating limitation of the Windows Forms TreeView.  It's even more frustrating because Microsoft claims this limitation is necessary for backwards compatibility (what a bunch of nonsense) and yet they turn right around and use treeviews in their software that behave like you would expect.

    In researching this issue you're likely to stumble across the page on "Customizing a Control's Appearance Using Custom Draw (http://msdn.microsoft.com/library/en-us/shellcc/platform/commctls/custdraw/custdraw.asp) and particularly the section titled "Changing fonts and colors".  This section gives an explanation for how to (one would think) overcome this difficulty with fonts by taking advantage of CustomDraw.  But if you read further you'll notice a snide remark that states "For Version 5.0, these two controls may display clipped text if you change the font by returning CDRF_NEWFONT. This behavior is necessary for backward compatibility with earlier versions of the common controls."

    The paragraph goes on to state that you can get around this ridiculous clipping behavior by sending a CCM_SETVERSION message to the control.  However, it fails to give any example of how to accomplish this task.  In fact, I noticed at least one developer had attempted to implement this suggestion and decided that it didn't work.  When I gave it a shot, I came to the same conclusion at first.  If you send this message from the constructor of a subclassed control as I was doing, for example, it really screws things up.  Instead, it appears that if you're careful to send the message after the underlying handle has been created, things start looking up.

    In the end, after writing hundreds of lines of code trying to get this working, I think have it mastered using just ten lines of code.  And I didn't even have to use CustomDraw.  As always, there's many ways this could be done, but here's one solution to this problem using a subclassed control.  Simply compile these *ten* lines of code into a custom control (this exmaple is written in Visual Basic .NET but could easily be translated into C#) and use this new control rather than the broken version of the TreeView shipped with Windows Forms:

    Public Class FixedTreeView
        Inherits TreeView

        Protected Overrides Sub OnCreateControl()
            MyBase.OnCreateControl()

            Const CCM_FIRST As Integer = &H2000
            Const CCM_SETVERSION As Integer = (CCM_FIRST + &H7)

            Dim msg As Message = Message.Create(Me.Handle, _
                                                CCM_SETVERSION, _
                                                IntPtr.op_Explicit(5), _
                                                IntPtr.op_Explicit(0))

            DefWndProc(msg)
        End Sub
    End Class

  • Julian Price

    I'm also interested in having a TreeView actually use the Font that is in the TreeNode as expected. Why have it otherwise

    Anyway... What I want to do with it is use a TreeView to show a hierarchy of reports, sections (header, footer, etc.), and fields -- the user can click on a field to set the Font and Color and I want the TreeNode text to reflect the selected Font (Color works fine of course). At this time I have to add the name and size of the Font to the text, e.g. "HeaderText -- Arial, 36pt", but I'd rather have the text rendered in the actual Font -- and not get clipped. So this would affect the NodeHeight as well.

  • Hamid H. Awan

    Hi, this is very usfull explanation
    I translated this code into C# but it doesn't work. When set font style to bold text is cliped. Could you help me

    public class TreeViewFixedBoldFonts : TreeView
    {
       protected override void OnCreateControl()
       {
         base.OnCreateControl ();

         const int CCM_FIRST  = 0x2000;
         const int CCM_SETVERSION = (CCM_FIRST + 0x0007);

        Message msg = Message.Create(this.Handle,CCM_SETVERSION,new IntPtr(5), new IntPtr(0));

        DefWndProc(ref msg);
       }
    }

  • MadhuSri

    A very easy quick fix is: 

    tn.NodeFont = new Font( this.Font.FontFamily, this.Font.Size, FontStyle.Bold );
    // HACK: Makes the entire Node text display, more or less.
    tn.Text = tn.Text + new String(' ', 10 );

    //in other words, Just add a space to the end, trim when necessary

  • cro

    Outlook Express is a good example of a treeview that uses bold nodes and regular nodes - it does not exhibit the clipping problem.

    How exactly do I "send a CCM_SETVERSION message with the wParam value set to 5" using VB.NET

    Thanks.

  • Mubarak Sha

    A number of methods to expose the drawing of treenode background, text, etc, are already on the drawing board <*** no guarantees ***> for our next release.  No easy workarounds I can think of.
  • gift

    I noticed that the message of TVN_SETDISPINFO to tell the TreeView the Text of TreeNode had been changed, so I do “treeNode.Text += “” ” in order to send the message TVN_SETDISPINFO, and it worked well, but I don’t know how to send the TVN_SETDISPINFO message.
  • Secrets

    I'm in a situation that the font of TreeNode can be Bold as well as Regular, does any one have ideas that the size of TreeNode can math with the text with special font(no larger or clipped)
  • yulia_o

    This isn't, technically, a bug.  The Windows treeview works the same way in that it creates all nodes based on the specified font of the treeview control.  They can be set individually, but, as you noticed, the space available is driven by the parameters at creation time.  To tell you the truth, I can't find an example within Windows where a treeview has contents that are bolded.  <a href="http://msdn.microsoft.com/library/en-us/shellcc/platform/CommCtls/CustDraw/CustDraw.asp">This</a> might be informative:

    "For Version 5.0 of the common controls, these two controls may display clipped text if you change the font by returning CDRF_NEWFONT. This behavior is necessary for backward compatibility with earlier versions of the common controls. If you want to change the font of a list-view or tree-view control, you will get better results if you send a CCM_SETVERSION message with the wParam value set to 5 before adding any items to the control."

  • David T92166

    Thank you very much.  Your post was very informative and the example code works wonderfully.

    Rick Boardman
    www.netbotics.com

  • Gary Cabana

    The way I fixed this problem is that in design time, I set the Treeview's font property to Bold.  This will cause all of the nodes you add to it to default to bold font, so you have to reset them back to regular font as you add them.  Then when you make them bold again, they will display properly.  Perhaps crude, but effective.


  • SSG31415926

    As good point on OE, I've used it so long I didn't even notice.

    So, yeah, this is weird and is a bug in that the selection rectangle doesn't resize based on the NodeFont.

    As for sending the message, it's certainly doable, but you'll need to create a derived treeview and, unfortunately, it doesn't work...  I just tried it with the following code:

        Public Class MyTreeView
            Inherits TreeView

            Public Sub New()
                MyBase.New()

                Const CCM_SETVERSION As Integer = &H2000& + 7
                Dim m As Message = Message.Create(Me.Handle, CCM_SETVERSION, IntPtr.op_Explicit(5), IntPtr.Zero)
                WndProc(m)
            End Sub

        End Class


    I am curious though...I'll play around when I have some time and see if I can come up with a workaround for you.

  • KeithGarrett

    Thank you for your efforts, I would very much appreciate a workaround to this problem!  

    Are the .NET development team aware of this issue and, if so, do they plan to fix it in a future release

  • Treeview Bug