开发者

Handling right-click within a MenuItem

Is it possible to check for a right-click on a menu item in .NET?

It appears开发者_C百科 that the framework doesn't expose it as an Event, but I've seen other applications (like Chrome and Firefox) which allow you to bring up a right-click context menu for a menu item. Presumably with a little event-loop magic you can do the same thing in .NET, right?

EDIT: I'm talking about desktop application programming, not ASP.NET.


It is an unnatural act. Menus are designed to automatically pop down when they lose the focus. The context menu will take the focus, end of menu. MenuStrip will fight you tooth and nail, I haven't seen it done.


In Winforms I'm unsure, I don't think so, the Click event is a generic EventHandler

In WPF, You can, the OnClick Event passes in a System.Windows.Input.MouseEventArgs object, which has properties on it like .MiddleButton .RightButton

For ASP.NET You need to use javascript to catch the right-click and submit a secret form, initiate a post-back, or use AJAX.


This isn't exactly what was asked for but is a decent compromise and isn't too big a stretch from the UI standards viewpoint. WinForms menus don't seem to even respond to right clicks so use the presence/absence of the shift key being pressed instead.

private void MenuClick(object sender, EventArgs args)
{
    if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift) DoSpecialStuff();
    else DoNormalStuff();
}


In Click event you can detect right mouse click with:

Control.MouseButtons == MouseButtons.Right;

Though you might want to check this also in Closing event so to prevent Close event from being raised.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜