C# Winforms bold treeview node doesn't show whole text

C#WinformsTreeview

C# Problem Overview


I'm using the following code to make my treenodes bold:

Font font = new Font(tvQuestionSequence.Font, FontStyle.Bold);

foreach (QuestionnaireBuilder_Category cat in categories)
{
    TreeNode node = new TreeNode();
    
    node.Text = cat.Description;
    node.Name = cat.Id.ToString();

    node.NodeFont = font;
    
    tvQuestionSequence.Nodes.Add(node);
}

But the text of the bold nodes is not displayed correctly. The last letter(s) are not shown. How come? And how to solve this problem?

C# Solutions


Solution 1 - C#

I found this Post when searching through the web because I am facing the exact same problem.

However, appending a white space to the end of the node was not an option, and I found an alternative way that seems to fix the issue.

After setting my node font Bold, all I need to do is reset the node text with the same value.

Here is the Code Sample:

Font boldFont = new Font(treeview.Font, FontStyle.Bold);
node.NodeFont = boldFont;
node.Text = node.Text;

It seems that the node is redrawn after changing the text, which is exactly what I wanted in the first place.

Solution 2 - C#

I've found that this is a Windows issue. A workaround for this problem is this:

In the form constructor set the font of the treeview to bold. When adding nodes which must not be bold, change the font to regular:

// Constructor of your form
public Form() 
{
    InitializeComponent();

    Font font = new Font(tvQuestionSequence.Font, FontStyle.Bold);
    tvQuestionSequence.Font = font;
}

// Add regular nodes (not bold)
Font font = new Font(tvQuestionSequence.Font, FontStyle.Regular);

TreeNode treeNode = new TreeNode();
treeNode.Text = "Foo";
treeNode.NodeFont = font;
    
TreeNode parent = tvQuestionSequence.Nodes.Find("parent", true);
parent.Nodes.Add(treeNode);
     

Solution 3 - C#

Simply use treeView.BeginUpdate() before you bold the node then treeView.EndUpdate() after you've bolded the node.

Solution 4 - C#

This is a known Windows bug. The simple solution is just to append an extra space character at the end of your strings. The space character will not be visible, but it will increase the number of pixels needed to draw the string, so the entire string will be visible.

Solution 5 - C#

This is all not helping for me. What DID the trick is making the font a little bigger and bold at DESIGN time. (In the Properties window)

So make sure you define the treeview with big enough font, then later you can add nodes with smaller font. They will fit.

Solution 6 - C#

I do agree with the solution provided. I just want to add to it to shed a little more light on what the problem is. The treeview has its own font which determines the width of items at the root level. That compensates for the fact that there is only an item height property available and no item width property.

The solution to your problem is to determine what the font of your root node should be, then set the tree to that same font. You can do that at design time also.

Hope that helps someone.

Solution 7 - C#

A workaround for this problem is this:

Set the defaul font of treeview to bold in the properties.

And chnage to not bold when you need.

Solution 8 - C#

I do the following, I set the DrawNode Event to call, it sets the node to bold and removes the highlighted colour.

You can set any colour you like using the first parameter of the e.Graphics.FillRectangle function.

private void SetNodeBoldWhenSelected(object sender, DrawTreeNodeEventArgs e)
{
    if (e.Node == null) return;
    var font = e.Node.NodeFont ?? e.Node.TreeView.Font;
    if (e.Node.IsSelected)
    {
        font = new Font(font, FontStyle.Bold);                
    }

    var bounds = new Rectangle( e.Bounds.X,e.Bounds.Y,e.Bounds.Width+20,e.Bounds.Height);

    e.Graphics.FillRectangle(SystemBrushes.ControlDarkDark, bounds);
    TextRenderer.DrawText(e.Graphics, e.Node.Text, font, bounds, SystemColors.HighlightText, TextFormatFlags.GlyphOverhangPadding);
}

Now when I select a node I get 20 pixels more space, for my font, this works well, one can calculate the "real" size needed but there is no specification stating it needs to do this but you can use Graphics.MeasureString if you feel you need to do that.

Solution 9 - C#

Very easy and works fine

treeView1.SelectedNode.NodeFont = new System.Drawing.Font(treeView1.SelectedNode.TreeView.Font, treeView1.SelectedNode.TreeView.Font.Style | FontStyle.Bold);
           this.treeView1.SelectedNode.Text += string.Empty;

Solution 10 - C#

I realize this is an old thread and it may have been answered. I just ran across this problem as I'm learning to use TreeViews. What worked for me was changing the font size for the entire TreeView to the same size, or bigger than the font of the level you want to bold. The default font size is 8.something. I changed mine to 10, which was the size I wanted my nodes, and the truncating was gone.

Solution 11 - C#

What worked for me: Hooking into the load event in the Control's constructor and tweaking the node as explained in BlunT's answer.

public MyControl()
{
    InitializeComponent();
    _head = new TreeNode();

    this.Load += (s, e) =>
    {
        trvParts.Nodes.Clear();
        _head.NodeFont = new Font(trvParts.Font, FontStyle.Bold);
        trvParts.Nodes.Add(_head);
        _head.Text = "Node Name";
    };
}

Solution 12 - C#

It's in vb.Net however the solution to re-enter the value of the TEXT field gets around this nicely. As in:

 With myNode
        Dim myText As String = .Text 'capture the text
        .NodeFont = New Font(<name of your treeview>.Font, FontStyle.Bold)
        .Text = myText 'reset the text to the original value
 End With

Solution 13 - C#

Based on MSDN Library, try change your code to:

Font font = new Font(tvQuestionSequence.Font, FontStyle.Bold);

foreach (QuestionnaireBuilder_Category cat in categories)
{
    TreeNode node = new TreeNode();

    node.Text = cat.Description;
    node.Name = cat.Id.ToString();

    node.NodeFont = font;

    tvQuestionSequence.BeginUpdate();  //added newline here  <--
    tvQuestionSequence.Nodes.Add(node);
    tvQuestionSequence.EndUpdate();  //added newline here <--
}

It work for me

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMartijnView Question on Stackoverflow
Solution 1 - C#Miguel Mesquita AlfaiateView Answer on Stackoverflow
Solution 2 - C#MartijnView Answer on Stackoverflow
Solution 3 - C#YoungStackerView Answer on Stackoverflow
Solution 4 - C#Adel HazzahView Answer on Stackoverflow
Solution 5 - C#BappieView Answer on Stackoverflow
Solution 6 - C#user3161309View Answer on Stackoverflow
Solution 7 - C#IsmaelView Answer on Stackoverflow
Solution 8 - C#Walter VerhoevenView Answer on Stackoverflow
Solution 9 - C#KilusView Answer on Stackoverflow
Solution 10 - C#user5868650View Answer on Stackoverflow
Solution 11 - C#sean.netView Answer on Stackoverflow
Solution 12 - C#planetbluView Answer on Stackoverflow
Solution 13 - C#Romi ArdiansyaView Answer on Stackoverflow