H. Dani
H. Dani

Reputation: 21

Visual Studio 2019 menustrip item click event

I've been working for a while on a school project which involves menustrip items in a C# windows forms application. I'm currently working in Visual Studio 2019. I've tried over and over again to find a way to conveniently add "click" event methods for each items of the menustrip and I have only found one solution via contextmenustrips from a youtube video (https://www.youtube.com/watch?v=UE5FuSjm3d0). The problem is that I have some menus with multiple items and the solution above doesn't work for such a case.

What am I missing? How can I add proper on_click or click events for the menustrip items?

enter image description here

Upvotes: 1

Views: 1772

Answers (3)

FunkyLobster27
FunkyLobster27

Reputation: 41

You cannot add a click handler from the Items Collection Editor as only the properties are available in this window.

Instead select your ToolStripMenuItem directly by name in the main (dockable) Properties window (View -> Properties Window or Alt+Enter)

If you look carefully there is a drop down box for the current item on the first line, in there you should be able to select your ToopStripMenuItems and add event handlers

Thanks to: https://www.telerik.com/forums/where-in-designer-is-the-click-event-hiding

Upvotes: 1

Ragnarokkr Xia
Ragnarokkr Xia

Reputation: 201

I personally recommend you to add the event handlers for menu strip items in your code manually.
Actually, there's no difference between adding it manually and using the IDE.
If you are familiar enough with the code, you can implement the handler in SomeForm.cs and register the handler to the event in SomeForm.Designer.cs then it will be EXACTLY the same with the code your IDE generate.
You can navigate to SomeForm.Designer.cs simply by pressing F12 when your cursor is on InitializeComponent() in the constructor of your form, or you can find it by expanding the form entry in the Solution Explorer.
If you insists on achieving what you want visually, you can find your menu strip item controls in a drop down list in a sub window called Properties, then you can add the event handlers in your preferred visual way.

Upvotes: 0

valeriof
valeriof

Reputation: 28

That is the proper way to do it from GUI. If you want to add the click events within the code, you can write the menu name, '.' and select Click, then type '+=':

mnuMyMenuItem.Click +=

VS will prompt you for an event handler, type TAB once to write the handler to the right side of the assignment, type TAB twice to automatically generate the method.

Upvotes: 0

Related Questions