如何设置TreeView选中节点的颜色 如题,,,用TreeNode的ForeColor属性是不行的,因为是选中的节点,而不是所有的节点,谢谢大家帮忙,,,在线等 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Tree1.SelectedNode.ForColor = Color.Black TreeView1.SelectedNode.ForeColor public Form1() { InitializeComponent(); treeView1.HideSelection = true; }TreeNode preNode = null; private void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e) { if (preNode != null) preNode.ForeColor = Color.Black; e.Node.ForeColor = Color.Blue; preNode = e.Node; } 最好关联一个ImageList控件这样通过图标更能直观。Tree1.SelectedNode.ImageIndex public class Form1 : Form { public Form1() { InitializeComponent(); //自已绘制 this.treeView1.DrawMode = TreeViewDrawMode.OwnerDrawText; this.treeView1.DrawNode+=new DrawTreeNodeEventHandler(treeView1_DrawNode); } //在绘制节点事件中,按自已想的绘制 private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e) { if ((e.State & TreeNodeStates.Selected) != 0) { //演示为绿底白字 e.Graphics.FillRectangle(Brushes.Green,e.Node.Bounds); Font nodeFont = e.Node.NodeFont; if (nodeFont == null) nodeFont = ((TreeView)sender).Font; e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.White, Rectangle.Inflate(e.Bounds, 2, 0)); } else { e.DrawDefault = true; } if ((e.State & TreeNodeStates.Focused) != 0) { using (Pen focusPen = new Pen(Color.Black)) { focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot; Rectangle focusBounds = e.Node.Bounds ; focusBounds.Size = new Size(focusBounds.Width - 1, focusBounds.Height - 1); e.Graphics.DrawRectangle(focusPen, focusBounds); } } } } Tree1.SelectedNode.ForColor = Color.Black 然后在设置之前将其他的都改成默认不就可以了 给你个遮盖的方法把 private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { TreeView _TreeView = (TreeView)sender; Control _SetLabel = _TreeView.Controls["SelectLab"]; if (_SetLabel == null) { Label _Label = new Label(); _Label.Name = "SelectLab"; _Label.AutoSize = false; _Label.BackColor = Color.Yellow; _Label.ForeColor = Color.Red; _Label.Font = _TreeView.Font; _TreeView.Controls.Add(_Label); _SetLabel = _Label; } _SetLabel.Size = new Size(e.Node.Bounds.Width + 5, e.Node.Bounds.Height); _SetLabel.Location = new Point(e.Node.Bounds.X, e.Node.Bounds.Y); _SetLabel.Text = e.Node.Text; } 谢谢8楼,,,大家应该去试试哈,,ForColor对选中节点字体颜色是不起作用的,,,结帐,,, tree1.TreeNodeStyle.Forecolor = color ; c# 序列化问题 产生字符串问题 首页只有<%@ Page %> 帮忙找一个C#与Access版的管理系统源代码 C#窗体代码问题 那位大哥可以帮我解决一下这个问题 刚从VB.NET 转到C#,C#的事件处理能象VB那样在上面选个对象,在选事件,然后写函数吗 搞到了几套微软的原版.net书,有需要的朋友来看 c#如何把控件添加到窗体边框上,像有道词典那样(如图) 关以资源回收 C# 与C++ 各有什么特点,二者的发展前景如何? 想个qq盗号思路,发现行不通,哪位高人指教下!
{
InitializeComponent();
treeView1.HideSelection = true;
}TreeNode preNode = null;
private void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{ if (preNode != null)
preNode.ForeColor = Color.Black;
e.Node.ForeColor = Color.Blue;
preNode = e.Node;
}
这样通过图标更能直观。
Tree1.SelectedNode.ImageIndex
public class Form1 : Form
{
public Form1()
{
InitializeComponent();
//自已绘制
this.treeView1.DrawMode = TreeViewDrawMode.OwnerDrawText;
this.treeView1.DrawNode+=new DrawTreeNodeEventHandler(treeView1_DrawNode);
} //在绘制节点事件中,按自已想的绘制
private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
{ if ((e.State & TreeNodeStates.Selected) != 0)
{
//演示为绿底白字
e.Graphics.FillRectangle(Brushes.Green,e.Node.Bounds); Font nodeFont = e.Node.NodeFont;
if (nodeFont == null) nodeFont = ((TreeView)sender).Font;
e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.White,
Rectangle.Inflate(e.Bounds, 2, 0));
}
else
{
e.DrawDefault = true;
} if ((e.State & TreeNodeStates.Focused) != 0)
{
using (Pen focusPen = new Pen(Color.Black))
{
focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
Rectangle focusBounds = e.Node.Bounds ;
focusBounds.Size = new Size(focusBounds.Width - 1,
focusBounds.Height - 1);
e.Graphics.DrawRectangle(focusPen, focusBounds);
}
} }
}
然后在设置之前将其他的都改成默认不就可以了
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
TreeView _TreeView = (TreeView)sender;
Control _SetLabel = _TreeView.Controls["SelectLab"];
if (_SetLabel == null)
{
Label _Label = new Label();
_Label.Name = "SelectLab";
_Label.AutoSize = false;
_Label.BackColor = Color.Yellow;
_Label.ForeColor = Color.Red;
_Label.Font = _TreeView.Font;
_TreeView.Controls.Add(_Label);
_SetLabel = _Label;
}
_SetLabel.Size = new Size(e.Node.Bounds.Width + 5, e.Node.Bounds.Height);
_SetLabel.Location = new Point(e.Node.Bounds.X, e.Node.Bounds.Y);
_SetLabel.Text = e.Node.Text;
}