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

ctreeviewwinforms

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?

Best Answer

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.

Related Topic