How can I open a context menu by left-click?

I  hope to open a context menu by left-clicking button of mouse, what do I do

 




Answer this question

How can I open a context menu by left-click?

  • Wolfgang W.

    Try this:


    public class Form1 : Form
    {
       private System.Windows.Forms.ContextMenu contextMenu1;

       private void InitializeComponent()
       {
          ...
       }

       protected override void OnMouseDown(MouseEventArgs e)
       {
          base.OnMouseDown(e);

          contextMenu1.Show(this, new Point(e.X, e.Y));
       }
    }


     



  • BDH

    Who know thanks!

  • Nima Dilmaghani

    Thank you very much!

    But, you know I add a ToolBar component in my form, there are many buttons in the ToolBar, I hope to open the context menu when I left-click a button, If I use you code, the context menu always is opened whichever button is left-clicked.
    Maybe what I need to know is how to get the cursor's position, in screen coordinates, but I don't know how to do. In Delphi ,maybe I can get it by using GetCursorPos , then change it to local coordinates, but In C#, I don't know how to do!
     

  • Ayman Shoukry

    Who know thanks!

  • How can I open a context menu by left-click?