在树视图中设置不同级别的不同颜色 [英] set different colors with different levels in treeview

查看:120
本文介绍了在树视图中设置不同级别的不同颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有treeview.this树视图以编程方式加载。这是3级树视图



部门

|

- 小部门

|

_KPI



实际上我想为每个nodelevel设置不同的字体和不同的颜色.ie

部门与Subdepartment和KPI相比有不同的字体,

与部门和KPI相比,Subdepartment有不同的字体,

KPI有与部门和子部门相比不同的字体

那么可以做什么?

实际上我已经尝试了但是只为选定的节点设置了字体和颜色。



Mycode:



treeViewKPI.SelectedNode.NodeFont = new Font(" Arial",10);

treeViewKPI.SelectedNode.ForeColor = System.Drawing.Color.Blue;

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;

推荐答案

树视图有很多自定义可用的选项。尝试使用

LevelStyles / LeafNodeStyle / NodeStyle / ParentNodeStyle / RootNodeStyle等标签。



您可以使用上面的属性设置不同的颜色和字体。
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.


您可以使用下面的代码更改颜色和字体。

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



但是在你的情况下你需要使用自己的逻辑找到Right节点的索引。


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


要枚举Win Forms TreeView中的所有TreeNode,并根据它们在TreeView中的级别分配不同的BackColor和Font:
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>
{
    // 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);
    }
}

此代码使用每个TreeNode的Level属性作为颜色和字体列表的索引。

This code uses the 'Level property of each TreeNode as an index into the lists of Colors and Fonts.


这篇关于在树视图中设置不同级别的不同颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆