views:

605

answers:

2

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?

A: 

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);
Martijn
+1  A: 

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.

Adel Hazzah