Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# menu WinForm
How to change the back color of menu item on mouse leave ?
Any help in this regard ?
Posted 9-Sep-12 19:43pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
just subscribe to the MouseLeave event for the desired MenuItem and use this code as an example:
private void toolStripMenuItem1_MouseLeave(object sender, EventArgs e)
{
   ((ToolStripMenuItem)sender).BackColor = Color.Red;
}
  Permalink  
Comments
Itz.Irshad at 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 at 10-Sep-12 1:32am
   
the default color is: System.Drawing.SystemColors.Control
Itz.Irshad at 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)

  Print Answers RSS
0 OriginalGriff 295
1 DamithSL 265
2 CPallini 235
3 Maciej Los 190
4 Sergey Alexandrovich Kryukov 174
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 10 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100