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

Viewed 32712

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?

13 Answers

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);

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.

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

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

Related