Change font of RadTreeView e.a.

4 posts, 0 answers
  1. Lucien
    Lucien avatar
    7 posts
    Member since:
    Aug 2010

    Posted 21 Oct 2011 Link to this post

    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.
  2. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 26 Oct 2011 Link to this post

    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.

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Lucien
    Lucien avatar
    7 posts
    Member since:
    Aug 2010

    Posted 04 Nov 2011 Link to this post

    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.
  5. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 09 Nov 2011 Link to this post

    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.

Back to Top