问题描述
我有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;
推荐答案
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.
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.
这篇关于在树视图中设置不同级别的不同颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!