To enumerate all the TreeNodes in the Win Forms TreeView, and assign different BackColor and Font based on their level in the TreeView:
private List<Color> colorList = new List<Color>
{
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)
{
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.