简体   繁体   中英

How can we highlight active menu-item on clicking menu strip item?

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

在此输入图像描述

and on click MenuStripItem I want to achieve this

在此输入图像描述

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

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.

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;
}

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM