As an epitaph to this question, you can do this work using normal .NET functions; you just need to go a little deeper into the event call stack. Instead of handling the MouseClick event, handle the MouseDown event. Lately, I had to do something similar, and I just redefined the OnMouseDown method instead of binding the handler. But the handler should work too. Here is the code:
protected override void OnMouseDown(MouseEventArgs e) { if (e.Button == MouseButtons.Right && !HandlingRightClick) { HandlingRightClick = true; if (!cmsRightClickMenu.Visible) cmsRightClickMenu.Show(this, e.Location); else cmsRightClickMenu.Hide(); } base.OnMouseDown(e); } protected override void OnMouseUp(MouseEventArgs e) { HandlingRightClick = false; base.OnMouseUp(e); } private bool HandlingRightClick { get; set; }
The HandlingRightClick property is to prevent many OnMouseDown logic triggers; the user interface will send several MouseDown messages, which may interfere with hiding the context menu. To prevent this, I execute the logic only once on the first MouseDown trigger (the logic is simple enough that I don't care if two calls happen, but you can), and then ignore any other MouseDown triggers until MouseUp happens. This is not ideal, but it will do what you need.
source share