Martijn Martijn - 1 month ago 23
C# Question

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

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?

Answer

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