C # Winforms bold treeview node does not display all text

I use 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 in bold is not displayed correctly. The last letter (s) is not displayed. How so? And how to solve this problem?

+50
c # winforms treeview
Feb 16 2018-10-16
source share
12 answers

I found that this is a problem with Windows. A workaround for this problem is this:

In the form constructor, set the font of the tree to bold. When adding nodes that should not be in bold, change the font to normal:

 // 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); 
+15
Feb 16 2018-10-16
source share

I found this Mail when searching the Internet because I was facing the same problem.

However, adding a space to the end of the node was not possible, and I found an alternative way to fix the problem.

After installing my node Bold font, all I need to do is reset the node text with the same value.

Here is a sample code:

 Font boldFont = new Font(treeview.Font, FontStyle.Bold); node.NodeFont = boldFont; node.Text = node.Text; 

It seems that node is redrawn after changing the text, which is exactly what I wanted in the first place.

+54
Apr 27 2018-11-11T00:
source share

This is a known Windows error. A simple solution is to simply add an extra space character at the end of your lines. The space character will not be visible, but it will increase the number of pixels needed to draw the line, so that the entire line will be visible.

+5
Jun 26 '10 at 4:00
source share

All this does not help me. What the trick did is make the font a little big and bold in DESIGN time. (In the Properties window)

So make sure you define a treeview with a large font, and then you can add nodes with a smaller font. They will do.

+3
Nov 13 '12 at 7:26
source share

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

The solution to your problem is to determine what the font of your root node should be, and then set the tree to the same font. You can do this during development too.

Hope this helps someone.

+3
Jan 04
source share

Very simple and works great

 treeView1.SelectedNode.NodeFont = new System.Drawing.Font(treeView1.SelectedNode.TreeView.Font, treeView1.SelectedNode.TreeView.Font.Style | FontStyle.Bold); this.treeView1.SelectedNode.Text += string.Empty; 
0
Jul 28
source share

A workaround for this problem is this:

Set the font defaul tree for bold in the properties.

And not to be bold if you need to.

0
Jul 15 '14 at 11:32
source share

I understand that this is an old thread, and it may have been answered. I just ran into this problem as I am learning to use TreeViews. For me, this changed the font size for the entire TreeView with the same size or larger than the level font you need. The default font size is 8.something. I changed mine to 10, which was the size of my nodes, and the truncation disappeared.

0
Feb 01 '16 at 16:11
source share

What worked for me: attaches to the load event in the Control constructor and sets up the node as described in the BlunT 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"; }; } 
0
Aug 24 '16 at 13:14
source share

In vb.Net however, the decision to re-enter the TEXT field value goes around this nicely. How 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
Oct 20 '16 at 15:10
source share

Just use treeView.BeginUpdate () before deleting the node, and then treeView.EndUpdate () after selecting the selected node area.

0
Dec 28 '16 at 21:10
source share

I do the following: I set the DrawNode event to call, it sets the node in bold and deletes the highlighted color.

You can set any color 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 node, I get 20 pixels more space, for my font it works well, you can calculate the required "real" size, but there is no specification indicating that this is necessary, but you can use Graphics.MeasureString if you consider what you need to do it.

0
Nov 22 '17 at 13:37
source share



All Articles