Friday, February 02, 2007

Of TreeView Nodes and their Fonts


If you ever happen to change (upgrade) the font (increase font size or make the font bold) of a node (Class TreeNode) in the TreeView (System.Windows.Forms.TreeView) and run your program, you will be sorry because the label of your node gets clipped!

The only way to work around this is apparently to set the font of your TreeView to the worst case condition; i.e. if you want to make some of the nodes bold then set the font to be bold. Change the font (from Bold to Regular) of the nodes when you actually add the nodes to the treeview.

Here is a quick example -

'Windows form designer generated code

'TreeView Initialization. I only want the root node to be bold, but I set the default FontStyle to Bold

Me.tvwItems.Font = New System.Drawing.Font _

("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Private Sub AddNodestoTreeView()

'Create font object to change FontStyle of the nodes other than the root node

Dim Font As Font

Font = New Font(tvwItems.Font, FontStyle.Regular)

'Child nodes added

'Change FontStyle for the nodes other than the root node

CurNode.NodeFont = Font

End Sub

I’d appreciate your inputs on smarter workarounds to this.

TIA!

No comments: