which event to use to get the menu strip item clicked?
I've tried the menuStrip_ItemClicked event of the menuStrip; but it fires only on the top menus only ( like "File", "Edit", "Windows").
I want to catch the sub menu items clicked event.
Thanks.
If you are talking about WindowsForms and use VisualStudio:
Go to design of your menu, rigth click on menu item you want, select properties, in property grid select events tab, select Click or MenuClick (don't remember precisely) event and double click on it.
Done
I think you should hook to click event of each ToolStripMenuItem. It can be the done with the same event handler if you prefer to have logic for it in one function.
Every subitem has its own event for clicking. In the winforms designer, you can just double click on the correct item you want to let happen. It will create a saveButton_clicked event or whatever event you would like and you can specify the information for that specific action in the method.
You can even let the shortcut commands work like CTRL+S when assigning them in the designer of visual studio.
Related
There are several KeyPress method in my code. All are working except the one due to no reference. I have checked the project for naming mistake. But there is no any mistake. How can I make reference for this method.
btnback_KeyPress is not working.
Looks like you just don't have the event hooked up. Go into the designer, select the btnback button, then in the Events tab of the Properties window, find the KeyPress event and subscribe the btnback_KeyPress method to it as a handler.
You need to wire the event to the button.
You can do so by selecting your button in the [Design] view and then selecting the Events tab (circled in red) from the properties view.
At KeyPress, enter your event name.
It looks like you're missing a line of code in the Form1.Designer (supposedly it's Form1). Look it up and add
this.btnback.KeyPress += new System.EventHandler(this.btnback_KeyPress);
I have a context menu with sub-menus that have a lot of items.So how can I use case switch or something like that for the click event of those items,instead of double clicking them from the designer?
Regards.
Send them to the same click event handler.
Then you can use the sender parameter to determine which object was clicked.
You can double click on first one (arbitrary) and after copy the name of generated event handler and paste it in the Click property of any other MenuItem form Property window.
I have a Panel to play media. How can I determine if it was Right clicked? I want to bring up a ContextualMenu on the panel with (Pause, Play, Stop and Start Over)
just wire up your ContextMenu to the Panels ContextMenu property... another option is assign your handler to the MouseClick event of the Panel and check the event args for Button == MouseButtons.Right.
In visual studio on your form, add a contextmenustrip, and populate it with the values that you want. The panel has a property called contextmenustrip, all you need to do is set that to the one that you create and visual studio will do the rest for you.
I don't do much WinFom development so I am not too familiar with the MenuStrip control. I have added a menu strip to my form and added (1) item to it. All of this was done using the designer.
So I have Utilities -> Download Utility. When I double click on 'Download' in the designer an event handler is created for me.
private void downloadUtilityToolStripMenuItem_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Ding!");
}
UPDATE:
I noticed that the IntializeComponent() in the constructor of my form never seems to be run. I have placed a breakpoint in the constructor and it never hits. I refactored this form to change the name from the default (form1) to 'main'. I assume this is the problem but I don't see why. All of the form1 references seemed to have been updated. I did this with the IDE.
When I debug this application I can never seem to get this event to fire. What am I missing here?
-Nick
Check on the property page of the menu item (under events - click the lightning icon) if the Click event has a handler.
Check:
Properties Window for the menu, click on the menu item in question for the 'Download'
Click on the 'Lightening Bolt', a small icon below the top of the Properties Window, if you were to mouse over it, it would display 'Events' in the tooltip.
Scroll down and look for the 'Click Event' under Actions, double click it, to let VS automatically fill in the event handler for you
OR
Double click on the menu item within the Forms Designer, that will default to the menu item's click event and fill in the code for the 'Download' Menu item, i.e. MessageBox.Show("Ding");
Hope this helps,
Best regards,
Tom.
I got it working. Apparently when debugging the project it wasn't rebuilding. After refactoring the name of my form it was necessary to 'Rebuild' the solution. Now all over my events work as they should. Thanks for the help.
I am writing an app where the user should be able to alter the action of a button.
The user should right-click a button, and choose an option from a pop-up context menu. Once the choice has been made the button will perform a different action when the user uses a normal click.
I've already gotten the "Click" event working for a normal button click, however the "MouseClick" event handler isn't working correctly.
The "MouseClick" event gets activated on regular left-clicks, but never get's called for right-click.
Is there some default event handling being performed that is ignoring that right-click?
I'm sorry to say that this would be a serious UI blooper. Perhaps it would make more sense to add a small combobox next to the button.
Perhaps something like this?
http://www.codeproject.com/KB/buttons/SplitButton.aspx
If you want to display a context menu with actions to choose from it should be enough to assign a ContextMenuStrip to the ContextMenuStrip property. There is usually no need to manually handle the mouse events for that.
surely it would be better to implement it on the MouseDown event rather than the MouseUp event. i dont understand how this is much different from MouseClick event
edit: Just tried this and it works a treat :)
In Button (and certain other controls), the MouseClick event is only fired for the left button. Refer to MSDN.
If you want to know about the right button, respond to the MouseUp event--though as other posters have pointed out, this isn't a great UI idiom.
Use the Mouse UP event... test that Button.X and Button.Y are within the size of the button otherwise you have moved the mouse away from the button.
Terry G