?? Multiple Selection Colors ??

  • Thread starter Thread starter Tom Baxter
  • Start date Start date
T

Tom Baxter

Hi everyone,

I am hoping this is possible. I have a WinForm TreeView. Some nodes in the
tree have a red or yellow background color (based on some business logic)
while other nodes have the default (white) background color. This part works
fine.

When the user selects a node, however, the background color of the node is
always SystemColors.Highlight (a dark blue) while the node is selected. I
would like the background color of red or yellow nodes to be something other
than dark blue when these nodes are selected. So basically, I am looking for
a way to have three different "highlight colors" when a node is selected. The
highlight color will be different for red, yellow or default nodes.

Setting the background property of a TreeNode will not work since this color
only applies when the node is unselected (that's how I got the nodes to have
red and yellow backgrounds).

Thank you
 
Dear Tom,

As I understand, you want to change the default highlight color for the
TreeView nodes.

This can be done by setting the DrawMode of TreeView to OwnerDrawAll and
handling the DrawNode event.

To better design the code, we can create a custom TreeNode class to add a
HighLightColor property, something like this:

public class MyTreeNode : TreeNode
{
private Color _highLightColor = SystemColors.Highlight;

public Color HighLightColor
{
get { return _highLightColor; }
set { _highLightColor = value; }
}
}

Then we can do something like this in the DrawNode event handler:

void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
MyTreeNode node = e.Node as MyTreeNode;

if (node != null && SystemColors.Highlight !=
node.HighLightColor)
{
Rectangle nodeBounds = e.Node.Bounds;

if (this.treeView1.ShowLines)
{
Rectangle treeLine = e.Bounds;
treeLine.Width = nodeBounds.Left;

//Draw node line
using (Pen p = new Pen(Color.Gray))
{
p.DashStyle =
System.Drawing.Drawing2D.DashStyle.Dot;
e.Graphics.DrawLine(p,
treeLine.X + treeLine.Width / 2 + 1,
treeLine.Y + treeLine.Height / 2,
treeLine.Right,
treeLine.Y + treeLine.Height / 2);
e.Graphics.DrawLine(p,
treeLine.X + treeLine.Width / 2 +1,
treeLine.Y,
treeLine.X + treeLine.Width / 2 +1,
treeLine.Bottom);
}
}
//Draw hight light
if ((e.State & TreeNodeStates.Selected) != 0)
{
using (SolidBrush br = new
SolidBrush(node.HighLightColor))
{
e.Graphics.FillRectangle(br, e.Node.Bounds);
}
}
else
{
using (SolidBrush br = new SolidBrush(node.BackColor))
{
e.Graphics.FillRectangle(br, e.Node.Bounds);
}
}
using (SolidBrush br = new SolidBrush(Color.Black))
{
e.Graphics.DrawString(e.Node.Text, this.treeView1.Font,
br, e.Node.Bounds);
}
}
else
{
e.DrawDefault = true;
}
}

To test the effect, you can put the following code in the Form_Load method

private void Form2_Load(object sender, EventArgs e)
{
this.treeView1.DrawNode += new
DrawTreeNodeEventHandler(treeView1_DrawNode);
this.treeView1.DrawMode = TreeViewDrawMode.OwnerDrawAll;

MyTreeNode RedNode = new MyTreeNode();
RedNode.HighLightColor = Color.Red;
RedNode.Text = "Red";
RedNode.BackColor = Color.Orange;
this.treeView1.Nodes.Add(RedNode);

MyTreeNode GreenNode = new MyTreeNode();
GreenNode.HighLightColor = Color.Green;
GreenNode.Text = "Green";
GreenNode.BackColor = Color.White;
this.treeView1.Nodes.Add(GreenNode);

MyTreeNode YellowNode = new MyTreeNode();
YellowNode.HighLightColor = Color.Yellow;
YellowNode.Text = "Blue";
YellowNode.BackColor = Color.SaddleBrown;
this.treeView1.Nodes.Add(YellowNode);

//Default node
this.treeView1.Nodes.Add("A default one");
}

Build and run the code, you should find that the the highlight color for
the first three nodes are different.

If you have any unclear about my sample, please feel free to let me know.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top