This is a migrated thread and some comments may be shown as answers.

Change font of RadTreeView e.a.

3 Answers 503 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Lucien
Top achievements
Rank 1
Lucien asked on 22 Oct 2011, 04:24 AM
Hi,

I am trying the do a few things and I don't seem to be able to do this with the WinForms RadTreeView control - or at least I don't know how:

1. Change the font. Seems simple, but I tried doing it in both the designer and in code (myTreeView.Font = new Font(...) etc), but no matter what I set it to, the treeview still shows the same font. What do I need to do to change this??

2. How to I remove/hide/decrease the left margin showing on all first level nodes? I am not using a root node and there's always a white margin to the left of each node.

Thanks!

Lucien Dol
New Zealand.

3 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 26 Oct 2011, 01:42 PM
Hello Lucien,

Thank you for writing.

You can achieve both of your requirements by handling the NodeFormatting event of RadTreeView. Here is a code snippet, which sets the Font of the ContentElement of the node, and also hides the LinesContainerElement, which is responsible for the lines in hierarchical trees and also it causing the space on the left side of the node:
Font f = new Font("Arial", 14);
void radTreeView1_NodeFormatting(object sender, Telerik.WinControls.UI.TreeNodeFormattingEventArgs e)
{
    //remove the lines container to reduce the space on the left of the nodes
    e.NodeElement.LinesContainerElement.Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
    //set font
    e.NodeElement.ContentElement.Font = f;
}

More information regarding node formatting, can be found in the following documentation article: http://www.telerik.com/help/winforms/treeview-working-with-nodes-formatting-nodes.html.

I hope that you find this information useful. Should you have any other questions, do not hesitate to contact us. 

All the best,
Stefan
the Telerik team

Q2’11 SP1 of RadControls for WinForms is available for download (see what's new); also available is the Q3'11 Roadmap for Telerik Windows Forms controls.

0
Lucien
Top achievements
Rank 1
answered on 05 Nov 2011, 12:19 AM
Thanks. That fixed the problem. If have modified it somewhat to ensure the lines (and the space for the lines) only disappear when it is a root node:

private void sitesTreeView_NodeFormatting(object sender, Telerik.WinControls.UI.TreeNodeFormattingEventArgs e)

{

var font = new Font("Microsoft Sans Serif", 10);

e.NodeElement.ContentElement.Font = font;

if(e.NodeElement.IsRootNode)

e.NodeElement.LinesContainerElement.Visibility = ElementVisibility.Collapsed;

}


Lucien.
0
Stefan
Telerik team
answered on 09 Nov 2011, 01:52 PM
Hello Lucien,

I am glad that I could help. Just one suggestion, place the initialization of the font outside of the NodeFormatting event handler, since this event is fired constantly and the current implementation may cause performance issues. 
 
Regards,
Stefan
the Telerik team

Q2’11 SP1 of RadControls for WinForms is available for download (see what's new); also available is the Q3'11 Roadmap for Telerik Windows Forms controls.

Tags
Treeview
Asked by
Lucien
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Lucien
Top achievements
Rank 1
Share this question
or