3

I am working on a desktop application in C# WinForms. I have used menustrip to navigate between different panels. The problem which I am facing is I cannot highlight the active color of the menustrip icon. A pictorial description will explain better what I want to achive.

This is my menu strip

enter image description here

and on click MenuStripItem I want to achieve this

enter image description here

In short I want to the menu strip item to stay highlighted when I press Click on it just like Search and Edit in the picture and afterwards if I click on New Customers then it must be highlighted as Search & Edit

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Ezaz
  • 183
  • 1
  • 11
  • 1
    Possible duplicate of [Change the border color of Winforms menu dropdown list](https://stackoverflow.com/questions/32307778/change-the-border-color-of-winforms-menu-dropdown-list) – Ňɏssa Pøngjǣrdenlarp Sep 23 '17 at 14:48
  • Would using a `ToolStrip` be a better option? You could then add toggle buttons onto the `ToolStrip` and set the button corresponding to the active panel to be `Checked` – erdomke Sep 23 '17 at 14:54

3 Answers3

2

You can use ToolStrip instead and set items Checked property to true. To do so, you can handle ItemClicked event of ToolStrip and check items this way:

private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
    foreach (ToolStripButton item in ((ToolStrip)sender).Items)
    {
        if (item != e.ClickedItem)
            item.Checked = false;
        else
            item.Checked = true;
    }
}

This way it shows a border around checked item. If for any reason you are not satisfied with appearance, you can simply customize the appearance of checked item by creating a custom renderer and assigning it as renderer of the ToolStrip this way:

public class MyRenderer : ToolStripProfessionalRenderer
{
    public MyRenderer() : base(new MyColorTable())
    {
    }
}

public class MyColorTable : ProfessionalColorTable
{
    public override Color ButtonCheckedGradientBegin
    {
        get { return ButtonPressedGradientBegin; }
    }
    public override Color ButtonCheckedGradientEnd
    {
        get { return ButtonPressedGradientEnd; }
    }
    public override Color ButtonCheckedGradientMiddle
    {
        get { return ButtonPressedGradientMiddle; }
    }
}

And assign the renderer in Load event of in constructor of your form after initialize components this way:

toolStrip1.Renderer = new MyRenderer();

This way, it shows the checked item as highlighted.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Thank you man it helped me a lot. It is better to use toolstrip instead of menustrip. Thank you – Ezaz Sep 23 '17 at 16:39
0

The selected item can be changed on Paint (not sure if there is more appropriate event):

public Form1()
{
    InitializeComponent();

    ToolStripItem activeToolStripItem = null;
    menuStrip1.Paint += (o, e) => activeToolStripItem?.Select();
    menuStrip1.ItemClicked += (o, e) => activeToolStripItem = e.ClickedItem;
}
Slai
  • 22,144
  • 5
  • 45
  • 53
0

Set the background colour of the clicked item on the MenuStrip as follows:

private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
    foreach (ToolStripMenuItem item in ((ToolStrip)sender).Items)
    {
        if (item != e.ClickedItem)
            item.BackColor = Color.White;
        else
            item.BackColor = Color.Cyan;
    }
}
Graham Laight
  • 4,700
  • 3
  • 29
  • 28