Can I set the location of a context menu to appear by the mouse cursor?

glasswizzard

Well-known member
Joined
Nov 22, 2019
Messages
126
Programming Experience
Beginner
I've set up a ContextMenuStrip to appear when right click on the form, attempting to make a right click context menu. The menu appears at the top left of the form though, is there a way I can make it appear at the mouse cursor?

Edit: It actually appear at the top left of the screen, not the form.
 
C#:
        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                contextMenuStrip1.Show(this, new Point(e.X, e.Y));
            }
        }
Use the MouseEventsArgs of the MouseUp on whatever control you want the menu to appear. Using system.drawing, you can set a point for the event args for the current mouse position. This will show the menu beside whatever control you right click on. Haven't tested it as I am pushed on time today, but its how I'd do it. Try it
 
Notice the second parameter on .Show(), takes a point or an x, y position. If you have contextMenuStrip1.Show(), type a comma between the ( ) and you will be greeted by intellisense which informs us what parameters the method takes.
 
You can set the ContextMenuStrip property of the form in designer, then you don't need code to show it.
 
You could set it in the form properties as outlined above :
Screenshot_51.jpg

But If you're like me and prefer to handcode your controls because the designer burned me to many times before, I'll stick with the manual approach. ;)

Declare:
        private ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
Add your items manually along with your event handler for mouse up event :
Add Item and custom event:
        private void Form1_Shown(object sender, EventArgs e)
        {
            contextMenuStrip.Items.Add("File");
            MouseUp += Form1_MouseUp;
        }
Mouse Up Event:
        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                contextMenuStrip.Show(this, new Point(e.X, e.Y));
            }
        }
Works grand. All the designer is doing is wiring up the code behind the scenes, code you refuse to write. Someday the designer will spas out and burn you, and from that day on, you will want to programmatically write everything.

Here are the docs for future readers : ContextMenuStrip Class (System.Windows.Forms)
 
Setting ContextMenuStrip property also includes child controls of form.
 
Indeed it does. But by allowing the designer to do it for you, maybe you could tell our OP and future readers how to go about having the context menu only appear on certain controls instead of all of them when using your current suggestion?
 
ContextMenuStrip is a Control property, so it can be set for only those controls. As such you can select multiple controls in form and set the common property value in one go also.
 
Last edited:
Back
Top Bottom