Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have treeview.this treeview loaded programatically.this is 3 level treeview

Department
|
- Subdepartment
|
_KPI

actually i want to set differnts fonts and different colors for each nodelevel.i.e
Department have different fonts as compare to Subdepartment and KPI,
Subdepartment have different fonts as compare to department and KPI,
KPI have different fonts as compare to department and Subdepartment
so what can do?
actually i have try it but font and color set for only selected node.

Mycode:

treeViewKPI.SelectedNode.NodeFont = new Font("Arial",10);
treeViewKPI.SelectedNode.ForeColor =System.Drawing.Color.Blue;
Posted
Updated 16-Feb-20 5:52am
Comments
BillWoodruff 2-Jan-14 8:10am    
Is this the Win Forms TreeView ?

To enumerate all the TreeNodes in the Win Forms TreeView, and assign different BackColor and Font based on their level in the TreeView:
C#
private List<Color> colorList = new List<Color>
{
    // or use Color.AliceBlue 
    // or Color.FromArgb(red value, green value, blue value)
    Color.FromArgb(0xfff1de), 
    Color.FromArgb(0xdbe7eb),
    Color.FromArgb(0xf2f1eb)
};

private List<Font> fontList = new List<Font>
{
    new System.Drawing.Font("Arial", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))),
    new System.Drawing.Font("Arial", 10.2F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))),
    new System.Drawing.Font("Arial", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)))
};

private void Form1_Load(object sender, EventArgs e)
{
    // other Load Event business ...

    // recursively enumerate the TreeNodes
    colorTreeViewNodes(treeView1.Nodes);
}

private void colorTreeViewNodes(TreeNodeCollection theNodes)
{
    foreach (TreeNode theNode in theNodes)
    {
        theNode.BackColor = colorList[theNode.Level];
        theNode.NodeFont = fontList[theNode.Level];

        if (theNode.Nodes.Count > 0) colorTreeViewNodes(theNode.Nodes);
    }
}
This code uses the 'Level property of each TreeNode as an index into the lists of Colors and Fonts.
 
Share this answer
 
Comments
Gauri Bharde 7-Jan-14 4:16am    
thanks!!!!
BillWoodruff 7-Jan-14 4:23am    
Glad you found my response helpful. Note that the Microsoft TreeView has some known problems when you assign multiple size Fonts based on Node Level: you may see the Node Text clipped in the larger size.

It's one of many reasons I switched to a 3rd. party commercial TreeView for WinForms.
Tree view have a lot of customization options available. try using
LevelStyles / LeafNodeStyle / NodeStyle / ParentNodeStyle / RootNodeStyle etc tags.

you can set different colors and Font using above properties.
 
Share this answer
 
Comments
BillWoodruff 2-Jan-14 7:20am    
These Properties are available for the TreeView in System.Web.UI.WebControls. Not available for the WinForms TreeView.
ravikhoda 2-Jan-14 7:46am    
you can change the color and font using below code.

Treeview.Nodes[0].backcolor = color.blue;
Treeview.Nodes[0].Nodefonts = your font;

however in your case you need to find the index of the Right node using your own logic.


you can change the color and font using below code.
C#
Treeview.Nodes[0].backcolor = color.blue;
Treeview.Nodes[0].Nodefonts = your font;

however in your case you need to find the index of the Right node using your own logic.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900