Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Change backcolor and forecolor when hovering menu items
 

Change backcolor and forecolor when hovering menu items

Hi,

I have been searching and I haven´t found an answer for my doubt.

I am having a problem with menustrip. It has a default colorswhenpointer is hovering its items and I would like to change this color and forecolor as well. I had a problem when changing backcolor for selected item but I solved in this way (this is an example for one item):

private

void mnuItemBusqueda_Click(object sender, EventArgs e)
{
this.mnuItemBusqueda.ForeColor = Color.FromArgb(72, 99, 2);
this.mnuItemBusqueda.BackColor = Color.FromArgb(243, 245, 192);
}

Could you help me?It´s very important to get this for me.

Thank you.

egurre_egurre  Monday, June 29, 2009 3:02 PM

Hi egurre_egurre,

You can add a Form in your project and paste the code snippet below:
System;

System.Collections.Generic;

System.ComponentModel;

System.Data;

System.Drawing;

System.Linq;

System.Text;

System.Windows.Forms;

WindowsFormsApplication2

public partial class Form3 : Form

{

public Form3()

{

InitializeComponent();

}

private void Form3_Load(object sender, EventArgs e)

{

//Add the menu strip.

MenuStrip menu = new MenuStrip();

this.Controls.Add(menu);

this.MainMenuStrip = menu;

//Add a menu.

MyMenuItem menuItem = new MyMenuItem();

menuItem.Offset = 6;

menuItem.Text = "File";

this.MainMenuStrip.Items.Add(menuItem);

//Add a sub menu.

MyMenuItem subItem = new MyMenuItem();

subItem.Offset = 34;

subItem.Text = "Open";

menuItem.DropDownItems.Add(subItem);

}

}

public class MyMenuItem : ToolStripMenuItem

{

//This paramter needs to be adjusted.

public int Offset { set; get; }

protected override void OnPaint(PaintEventArgs e)

{

base.OnPaint(e);

if (this.Selected)

{

e.Graphics.FillRectangle(Brushes.Green, e.ClipRectangle);

Rectangle rect = e.ClipRectangle;

rect.X += Offset;

rect.Width -= Offset;

StringFormat sf = new StringFormat();

sf.LineAlignment = StringAlignment.Center;

e.Graphics.DrawString(this.Text, this.Font, Brushes.Black, rect, sf);

}

}

}

Besides, we need to pay attention to the Offset property, its value needs to be modified based on the menu level. For example, the main menu and its sub menu would be different.

Please feel free to tell me if my answer doesn’t satisfy you or you need more help.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, July 02, 2009 3:53 PM


        private void Form1_Load(object sender, EventArgs e)
        {
            this.newToolStripMenuItem.MouseHover += new EventHandler(changeColor);
            this.newToolStripMenuItem.MouseLeave += new EventHandler(revertColor);
            this.editToolStripMenuItem.MouseHover+=new EventHandler(changeColor);
            this.editToolStripMenuItem.MouseLeave += new EventHandler(revertColor);
        }

       

        private void revertColor(object sender, EventArgs e)
        {
            ToolStripMenuItem temp = (ToolStripMenuItem)sender;
            
            temp.BackColor = Control.DefaultBackColor;
            temp.ForeColor = Control.DefaultForeColor;
        }

        private void changeColor(object sender, EventArgs e)
        {
            ToolStripMenuItem temp = (ToolStripMenuItem)sender;
            temp.BackColor = Color.AliceBlue;
            temp.ForeColor = Color.Red;
            
        }



Thanks

- Omie
Someone makes my heart skip a clock cycle !
  • Edited byOmie Monday, June 29, 2009 8:29 PM
  •  
Omie  Monday, June 29, 2009 8:27 PM
Hi Omie!

I have the same problem as egurre_gurre has. I have tried the snippet code you have provided but hasn´t been useful to me. I thought use MouseEnter and MouseLeave functions to change backcolor and forecolor but it has a strange problem. Styles are not applied to its own tab. Let´s see an example:

If I apply styles to itself item doesn´t apply any backcolor:

private void Item1_MouseEnter(object sender, EventArgs e)
{
this.Item1.BackColor = Color.Black;
this.Item1.ForeColor = Color.White;
}

void Item1_MouseLeave(object sender, EventArgs e)
{
this.Item1.BackColor = Color.Transparent;
this.Item1.ForeColor = Color.Red;
}

But, if I apply styles to another item it works perfectly!! like this:

private
void Item1_MouseLeave(object sender, EventArgs e)
{
this.Item2.BackColor = Color.Transparent;
this.Item2.ForeColor = Color.Red;
}

void Item1_MouseEnter(object sender, EventArgs e)
{
this.Item2.BackColor = Color.Black;
this.Item2.ForeColor = Color.White;
}


Item2 when hovering are well applied styles!!I have no idea why this happens.

Anyway, if someone knows how to do hover style in another way I would be very grateful.

Thank you.



  • Edited byel_ninho Tuesday, June 30, 2009 3:56 PM
  • Edited byel_ninho Tuesday, June 30, 2009 3:58 PM
  •  
el_ninho  Tuesday, June 30, 2009 3:08 PM

Hi egurre_egurre,

From my experience, there is no method to directly modify the back color of a selected menu item, you need to create a custom menu item and redraw the background.

Please read this thread to get more help:
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/3384f32d-0993-4377-b8df-b6c8f283a7d0.

Best regards,
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, July 01, 2009 6:04 AM

Hi egurre_egurre,

From my experience, there is no method to directly modify the back color of a selected menu item, you need to create a custom menu item and redraw the background.

Please read this thread to get more help:
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/3384f32d-0993-4377-b8df-b6c8f283a7d0.

Best regards,
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.

Hi Aland Li,

I have checked the thread and I am having a problem. I would like to use the snippet code it is in the thread (it is marked as answer) but I don´t know how to call it. I have no idea what I must do to call to the function in order to set its properties.

Do you know how?

Thanks!
egurre_egurre  Thursday, July 02, 2009 12:00 PM

Hi egurre_egurre,

You can add a Form in your project and paste the code snippet below:
System;

System.Collections.Generic;

System.ComponentModel;

System.Data;

System.Drawing;

System.Linq;

System.Text;

System.Windows.Forms;

WindowsFormsApplication2

public partial class Form3 : Form

{

public Form3()

{

InitializeComponent();

}

private void Form3_Load(object sender, EventArgs e)

{

//Add the menu strip.

MenuStrip menu = new MenuStrip();

this.Controls.Add(menu);

this.MainMenuStrip = menu;

//Add a menu.

MyMenuItem menuItem = new MyMenuItem();

menuItem.Offset = 6;

menuItem.Text = "File";

this.MainMenuStrip.Items.Add(menuItem);

//Add a sub menu.

MyMenuItem subItem = new MyMenuItem();

subItem.Offset = 34;

subItem.Text = "Open";

menuItem.DropDownItems.Add(subItem);

}

}

public class MyMenuItem : ToolStripMenuItem

{

//This paramter needs to be adjusted.

public int Offset { set; get; }

protected override void OnPaint(PaintEventArgs e)

{

base.OnPaint(e);

if (this.Selected)

{

e.Graphics.FillRectangle(Brushes.Green, e.ClipRectangle);

Rectangle rect = e.ClipRectangle;

rect.X += Offset;

rect.Width -= Offset;

StringFormat sf = new StringFormat();

sf.LineAlignment = StringAlignment.Center;

e.Graphics.DrawString(this.Text, this.Font, Brushes.Black, rect, sf);

}

}

}

Besides, we need to pay attention to the Offset property, its value needs to be modified based on the menu level. For example, the main menu and its sub menu would be different.

Please feel free to tell me if my answer doesn’t satisfy you or you need more help.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, July 02, 2009 3:53 PM
Thank you very very much Aland Li!

It is working perfectly ;)
egurre_egurre  Friday, July 03, 2009 8:17 AM

You can use google to search for other answers

Custom Search

More Threads

• Control does not support transparent background colors,vb.net
• Collection returning no reference
• Ressources embedded problem with a compilation on the fly
• editor in tab view
• There is already a command handler for the menu command '5efc7975-14bc-11cf-9b2b-00aa00573819 : 17'
• Can you solve this simple problem please ?
• Rendering controls in VS Designer can take up to several minutes in VS 2008.
• Form Designer nowhere to be found - new to C#
• expandable property in property window
• design an advanced form class and edit it in design view