56

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?

Martijn
  • 22,949
  • 59
  • 161
  • 247

13 Answers13

64

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.

Miguel Mesquita Alfaiate
  • 2,361
  • 4
  • 22
  • 50
  • 13
    I had to set the font, add the node to the tree AND THEN reset the text to get this to work. If I set the font, and reset the text before adding it to the tree the text is still truncated. – Ben Tidman Feb 13 '12 at 18:55
  • 1
    This is a slightly better fix then the existing answer because it corrects the sizing issue instead of simply oversizing the rendering area of the nodes with a wider font. It is also easy to implement if you already use and inherited treenode and can simply shadow the property to re-apply the text if the font is bolded. This fixed a very frustrating issue I have been fighting with for a long time, thanks. – DarrenMB Sep 16 '15 at 18:07
  • This method works mostly when a tree view is visible. For some tree views it does not work. A workaround is -- change the node text when the tree view is already visible. – Maris B. Apr 21 '21 at 14:43
21

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
  • 22,949
  • 59
  • 161
  • 247
  • bear in mind that `Font` implements `IDisposable` so it should always have it's `Dispose` method called when it is no longer required. for this reason you should probably create your `Font` as a field and ensure you call it's `Dispose` method in the `Dispose` method of the `Form` – Adam Ralph Nov 05 '10 at 11:53
  • @AdamRalph, wouldn't `font`'s Dispose method get automatically called on exiting the Contructor or am I misunderstanding something? – Chris Pfohl Nov 24 '10 at 20:47
  • 3
    I think in the above example the font is stored as a property of the treeview, the treeview will be disposed by the form, the treeview will then dispose of the font in turn. I assume this is the case because the forms designer assigns new fonts to controls and does not dispose of them. – DarrenMB Sep 16 '15 at 18:03
  • Simply use treeView.BeginUpdate() before you change node font then treeView.EndUpdate() after you've changed node font – YoungStacker Dec 28 '16 at 21:09
7

Simply use treeView.BeginUpdate() before you bold the node then treeView.EndUpdate() after you've bolded the node.

YoungStacker
  • 168
  • 2
  • 8
5

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
  • 8,107
  • 2
  • 18
  • 16
3

This is all not helping for me. What DID the trick is making the font a little bigger and bold at DESIGN time. (In the Properties window)

So make sure you define the treeview with big enough font, then later you can add nodes with smaller font. They will fit.

Bappie
  • 31
  • 1
3

I do agree with the solution provided. I just want to add to it to shed a little more light on what the problem is. The treeview has its own font which determines the width of items at the root level. That compensates for the fact that there is only an item height property available and no item width property.

The solution to your problem is to determine what the font of your root node should be, then set the tree to that same font. You can do that at design time also.

Hope that helps someone.

3

A workaround for this problem is this:

Set the defaul font of treeview to bold in the properties.

And chnage to not bold when you need.

Ismael
  • 319
  • 3
  • 7
1

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.

Walter Vehoeven
  • 2,461
  • 14
  • 28
0

Very easy and works fine

treeView1.SelectedNode.NodeFont = new System.Drawing.Font(treeView1.SelectedNode.TreeView.Font, treeView1.SelectedNode.TreeView.Font.Style | FontStyle.Bold);
           this.treeView1.SelectedNode.Text += string.Empty;
Kilus
  • 1
  • 2
    The SO community prefer explanation on why and how you are solving a problem that way. Thus, if you could explain why you are doing this, your post would be way more valuable. – ForceMagic Oct 11 '12 at 04:04
0

I realize this is an old thread and it may have been answered. I just ran across this problem as I'm learning to use TreeViews. What worked for me was changing the font size for the entire TreeView to the same size, or bigger than the font of the level you want to bold. The default font size is 8.something. I changed mine to 10, which was the size I wanted my nodes, and the truncating was gone.

0

What worked for me: Hooking into the load event in the Control's constructor and tweaking the node as explained in BlunT's answer.

public MyControl()
{
    InitializeComponent();
    _head = new TreeNode();

    this.Load += (s, e) =>
    {
        trvParts.Nodes.Clear();
        _head.NodeFont = new Font(trvParts.Font, FontStyle.Bold);
        trvParts.Nodes.Add(_head);
        _head.Text = "Node Name";
    };
}
sean.net
  • 715
  • 7
  • 24
0

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
0

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

  • Thank you for this code snippet, which might provide some limited short-term help. A proper explanation [would greatly improve](https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made – Shawn C. Jan 26 '18 at 14:20