6

I am experiencing the well known issue where after setting a TreeNode's font to bold, the text of the TreeNode gets truncated. However, I believe I have found a situation in which all of the commonly accepted "fixes" fail to work.

Common Solution: http://support.microsoft.com/kb/937215

node.NodeFont = new System.Drawing.Font(node.NodeFont, FontStyle.Bold);
node.Text += string.Empty;

Variation 1: C# Winforms bold treeview node doesn't show whole text (see BlunT's answer)

node.NodeFont = new System.Drawing.Font(node.NodeFont, FontStyle.Bold);
node.Text = node.Text;

Variation 2: http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/acb877a6-7c9d-4408-aee4-0fb7db127934

node.NodeFont = new System.Drawing.Font(node.NodeFont, FontStyle.Bold);
treeView1.BackColor = treeView1.BackColor;      

Scenario In Which Above Fixes Do Not Work:

If the code that sets the node to bold is in the constructor (either of a form or, in this case, a user control), the fixes will not work:

public partial class IncidentPlanAssociations : UserControl
{
    public IncidentPlanAssociations()
    {
        InitializeComponent();

        TreeNode node = new TreeNode("This is a problem.");
        node.NodeFont = new Font(treeView1.Font, FontStyle.Bold);
        treeView1.Nodes.Add(node);

        // This does not fix problem
        node.Text += string.Empty;

        // This does not fix problem
        node.Text = node.Text;

        // This does not fix problem
        treeView1.BackColor = treeView1.BackColor;

    }
}

However, if I place any of these three "fixes" in code behind a button and click it after everything runs it will work just fine. I'm sure this is something to do with when the treeview is initially drawn or something, but I'm trying to figure out a good way around it. Any suggestions?

Community
  • 1
  • 1
Joe DePung
  • 991
  • 3
  • 11
  • 21
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Oct 23 '12 at 17:09
  • Try calling `treeview1.Refresh()` as the last line of code in your constructor. – Robert Harvey Oct 23 '12 at 17:09
  • @RobertHarvey, thanks for the suggestion. The call to Refresh() did not make a difference. – Joe DePung Oct 23 '12 at 17:45
  • 1
    Right. Try putting your code into the `Load` event of the user control, instead of the constructor. – Robert Harvey Oct 23 '12 at 18:23
  • Alright!! Good call. The bug workaround works perfectly now by moving that code to the Load event. I'm happy to mark as answer if you would like to actually respond with an answer. – Joe DePung Oct 23 '12 at 20:16
  • The link http://support.microsoft.com/kb/937215 is dead. – Wollmich Oct 30 '17 at 12:03

4 Answers4

8

Thanks @Robert Harvey for the assistance.

In case anyone is experiencing a similar issue, the workaround here was to move the code from the Constructor to the User Control's Load event. Any of the three variations above then work.

I personally chose to go with:

private void IncidentPlanAssociations_Load(object sender, EventArgs e)
{
    TreeNode node = new TreeNode("This is no longer a problem.");
    node.NodeFont = new Font(treeView1.Font, FontStyle.Bold);
    treeView1.Nodes.Add(node);

    // This fixes the problem, now that the code 
    //      is in the Load event and not the constructor
    node.Text = node.Text;

}

The code just needed to be in the Load event and not in the constructor for this workaround to the well-known bug to work correctly for me.

Joe DePung
  • 991
  • 3
  • 11
  • 21
0

If you assign the largest font that you are going to use (the BOLD and/or largest point size) to the base TreeView control, then explicitly change the font to a smaller font for your standard sized rows, you won't have the truncation problem.

In addition, everything will be nicely spaced, if you choose the largest font as not being too far removed from the standard font.

JTK
  • 1
0

Use treeView.BeginUpdate() and treeView.EndUpdate() in any visual changes.

Example:

this.treeView.BeginUpdate();
this.treeView.Nodes.Clear();
this.treeView.Nodes.AddRange(allNodes.ToArray());
this.treeView.EndUpdate();
Thiago Falcao
  • 4,463
  • 39
  • 34
0

I understand this is a very old post but I just found a solution that works for me which I found HERE. Apparently by setting the back color to empty it resolves the issue of the Bolded text being cut off.

Here is the fix:

treeView.BackColor = Color.Empty;
Mark Kram
  • 5,672
  • 7
  • 51
  • 70