1

There is one ToolStripMenuItem object which is containing other 3 ToolStripMenuItems on dropdown. I want use different hightlight colors on MouseHover of different ToolStripMenuItems. Say, For Yes - Green. For No - Red. For MayBe - Blue. The color of highlight should get change accordingly as i move among different items. Please help.

P.S. - MouseEnter, MouseLeave, MouseHover events aren't working because they work on ToolStripMenuItems and it is the case of Highlighter.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Arpit Gupta
  • 1,209
  • 1
  • 22
  • 39
  • Here is a [similar question](http://stackoverflow.com/a/9260827/5753992) regarding to 'How to change menu hover color?' – Lester May 04 '16 at 14:10
  • 1
    @LarsTech The linked post is not duplicate at all. The OP wants to use **different Highlight color for different ToolStripMenuItems**. The linked post doesn't satisfy such requirement and provides a way to change highlight color for all items. – Reza Aghaei May 04 '16 at 15:57

1 Answers1

3

When you are using a ToolStripProfessionalRenderer, changing back color doesn't change highlight color. Also while your tool strip uses a single renderer, since the renderer uses MenuItemSelectedColor property of ProfessionalColorTable of renderer, it can't render highlight color based on a condition.

You can change renderer of ToolStrip dynamically and set it's renderer to a renderer which draws highlight in your desired color.

enter image description here

Here are the steps which I used:

(1) Set the RenderMode of your ToolStrip to ManagerRenderMode:

this.toolStrip1.RenderMode = ToolStripRenderMode.ManagerRenderMode;

(2) Create a custom color table which you can pass MenuItemSelectedColor to it:

public class MyColorTable : ProfessionalColorTable
{
    private Color menuItemSelectedColor;
    public MyColorTable(Color color): base()
    {
        menuItemSelectedColor = color;
    }
    public override Color MenuItemSelected
    {
        get { return menuItemSelectedColor; }
    }
}

(3) Define renderers which you need at form level:

ToolStripProfessionalRenderer r = 
    new ToolStripProfessionalRenderer(new MyColorTable(Color.Red));
ToolStripProfessionalRenderer g = 
    new ToolStripProfessionalRenderer(new MyColorTable(Color.Green));
ToolStripProfessionalRenderer b = 
    new ToolStripProfessionalRenderer(new MyColorTable(Color.Blue));

(4) Handle MouseEnter and MouseLeave event for all your menu items which you want to use custom renderer and assign this handlers to all of them:

private void menuItem_MouseEnter(object sender, EventArgs e)
{
    var item = (ToolStripMenuItem)sender;
    if (item.Text == "Yes")
        ToolStripManager.Renderer = g;
    else if (item.Text == "No")
        ToolStripManager.Renderer = r;
    else if (item.Text == "MayBe")
        ToolStripManager.Renderer = b;
    else
        ToolStripManager.Renderer = null; // or use your default renderer
}

private void menuItem_MouseLeave(object sender, EventArgs e)
{
    ToolStripManager.Renderer = null; // or use your default renderer
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • This solution works, But the problem now is - That if i am going from first dropdown to second, and then second to third. Now in third dropdown only, I want this functionality while it is highlighting all the other dropdowns. – Arpit Gupta May 05 '16 at 04:30
  • So, The problem actually lies in Nested MenuItems. I don't want to highlight the parent items. – Arpit Gupta May 05 '16 at 06:35
  • The link of image is broken. But pay attention to handle enter and leave events only for those items which you need the behavior and not for all items. – Reza Aghaei May 05 '16 at 09:19