Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to change the back color of menu item on mouse leave ?
Any help in this regard ?
Posted

1 solution

Hi,

just subscribe to the MouseLeave event for the desired MenuItem and use this code as an example:
C#
private void toolStripMenuItem1_MouseLeave(object sender, EventArgs e)
{
   ((ToolStripMenuItem)sender).BackColor = Color.Red;
}
 
Share this answer
 
Comments
Itz.Irshad 10-Sep-12 1:26am    
Your answer is much accurate. But, I've something more to do. Actually, On sub-menu Item I have created a rectangle with x as a representation of close button. So, I've changed that rectangle back color to blue (highlight) on mouseover. Now, I need to change that color to default(previous). How can I do this? Code is attached below:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);

SolidBrush brush;
Rectangle r = new Rectangle(this.Bounds.Width - 20, 2, 16, 17);

// If click on Del(Close Icon)
if (bOnDel)
{
brush = new SolidBrush(Color.LightBlue);
e.Graphics.FillRectangle(brush, r);
brush.Color = Color.Blue;
e.Graphics.DrawRectangle(new Pen(brush, 1), r);
}
// If didn't click on Del(Close Icone)
if (!bOnDel)
{

brush = new SolidBrush(Color.FromKnownColor(KnownColor.Transparent));
e.Graphics.FillRectangle(brush, r);
brush.Color = Color.FromKnownColor(KnownColor.Transparent);
e.Graphics.DrawRectangle(new Pen(brush, 1), r);
}

//Code for Drawing Cross Lines

brush = new SolidBrush(Color.Gray);
Rectangle rCross = new Rectangle(this.Bounds.Width - 15, 8, 6, 6);
e.Graphics.DrawLine(new Pen(brush, 2), new Point(rCross.Right, rCross.Top), new Point(rCross.Left, rCross.Bottom));
e.Graphics.DrawLine(new Pen(brush, 2), new Point(rCross.Left, rCross.Top), new Point(rCross.Right, rCross.Bottom));
}
JF2015 10-Sep-12 1:32am    
the default color is: System.Drawing.SystemColors.Control
Itz.Irshad 10-Sep-12 1:41am    
Your're right. But, when we do mouseover the menu item its color changed from default to light yellow or mic of (Yellow and Some other color) in default behavior of menu tool strip. So, How I can update my run-time drawn rectangle color to that color on leaving mouse from menu item ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900