Friday, September 5, 2008

TreeView node font problem

Recently I encountered the following problem. I needed to set the font style of some nodes in a treeview at runtime to bold; but after setting the NodeFont property of the node the font changed correctly, but the text of the node was cut off.

This screenshot shows the problem:


The solution to the problem is to add an empty string to the Text property after setting the NodeFont property:

this.treeView1.SelectedNode.NodeFont = new Font(this.treeView1.Font, FontStyle.Bold);
this.treeView1.SelectedNode.Text += string.Empty;


The problem is also known at Microsoft and I found this workaround here.

This screenshot shows the correct text:

4 comments:

Unknown said...

Thanks a lot !!

I was facing this problem from many months !

Unknown said...

Sorry, but your solution doesn't work for root node.
Any other tip in that case?

Anonymous said...

I was having the same issue. I just changed the order of my statements and it fixed the problem.

From:

tvMessages.Nodes("Messages").Nodes("CIC").Nodes("Hits").Text = String.Format("Hits ({0})", _controller.UnreadCICHitCount.ToString)
tvMessages.Nodes("Messages").Nodes("CIC").Nodes("Hits").NodeFont = New Font(tvMessages.Font, FontStyle.Bold)
tvMessages.Nodes("Messages").Nodes("CIC").Nodes("Hits").ForeColor = Color.Red

To:

tvMessages.Nodes("Messages").Nodes("CIC").Nodes("Hits").NodeFont = New Font(tvMessages.Font, FontStyle.Bold)
tvMessages.Nodes("Messages").Nodes("CIC").Nodes("Hits").ForeColor = Color.Red
tvMessages.Nodes("Messages").Nodes("CIC").Nodes("Hits").Text = String.Format("Hits ({0})", _controller.UnreadCICHitCount.ToString)

Anonymous said...

customize treeview nodes for .net application