Search code examples
visual-studiowinformsevents.net-5toolstrip

How to create an event handler for a MenuItem on a ToolStripDropDownButton?


I'm using a ToolStrip control as a menu on a form. I have configured it with the Items Collection Editor. It seems that events cannot be configured in the Items Collection Editor. The ToolStrip itself has the Click and the ItemClicked events.

My menu contains a DropDownButton Tools. Tools contains a MenuItem Config. I need an event fired when I click Config.

The events Click and ItemClicked are just fired when I click Tools but not when I click the MenuItem Config.

I have not found any help on this simple task. Probably it's too simple to be discussed.

Can anyone give me a hint, please?


Solution

  • To generate the event handler using the designer, you may follow these steps:

    1. Go to the Properties window (or press F4).
    2. Open the drop-down that lists all the controls on your form and locate the MenuItem.
    3. Click on "Events" (the ⚡ icon).
    4. Find the event you need to use and double-click on it.

    Here's a demo:

    Find the MenuItem control and generate an event handler

    Alternatively, you may create the event handler yourself using code. For example, add this line to your form constructor:

    toolStripMenuItem1.Click += ToolStripMenuItem1_Click;