Search code examples
c#winformsmenustrip

Setting menu item state on a MenuStrip


In Visual C++ MFC there is an inbuilt mechanism for setting the menu item states. I am trying to do the same with C# and a WindowsForm object.

I found this which is not quite the same:

Grey out menustrip items when certain forms are open/active/focused

Here is my menu structure:

Sub menu

So, I decided to try this:

private void viewToolStripMenuView_Click(object sender, EventArgs e)
{
    zoomExtentsToolStripMenuItem.Enabled = viewCtrl != null;
}

It kind of works. But I am a bit picky. I can see the menu displayed with the item enabled and then I see it change to disabled.

What is the right way to set the menu item states before the menu is displayed? I know this sounds like a simple issue but I can't find the equivalent methodology to ON_UPDATE_COMMAND_UI.


Solution

  • I was using the wrong event handler!

    private void viewToolStripMenuView_DropDownOpening(object sender, EventArgs e)
    {
        zoomExtentsToolStripMenuItem.Enabled = viewCtrl != null;
    }