Question

I want to click on a button and have it show a ContextMenuStrip right below the button. It keeps showing up in the left hand side of the screen when i try PointToScreen and top and left coordinates.

Any suggestions?

Was it helpful?

Solution 2

I figured it out:

layoutMenus.Show(Cursor.Position.X, Cursor.Position.Y);

OTHER TIPS

I know this is an old question but I think it may help other people. Following code will display the context menu just below the button being clicked and the button will look like a dropdown button.

private void Button1_Click(object sender, EventArgs e)
{
    Button btnSender = (Button)sender;
    Point ptLowerLeft = new Point(0, btnSender.Height);
    ptLowerLeft = btnSender.PointToScreen(ptLowerLeft);           
    ctMenuStrip.Show(ptLowerLeft);
}

ContexMenuName under button, aligned to the right side of button (expands to below button and to the left): ContexMenuName.Show(ButtonName, new Point(ButtonName.Width - ContexMenuName.Width, ButtonName.Height)); Hope this will help sb :)

As far as I know the code you require was here:

// On the right of the button

ContextMenuName.Show(ButtonName.Left + ButtonName.Width + this.Left, ButtonName.Top + this.Top);

At the bottom of the button

ContextMenuName.Show(ButtonName.Left + this.Left, ButtonName.Top + ButtonName.Height + this.Top);

At the bottom right of the button

ContextMenuName.Show(ButtonName.Left + ButtonName.Width + this.Left, ButtonName.Top + ButtonName.Height + this.Top);

Make sure that when you're positioning the context menu that you pass it the correct screen coordinates. You'll need to use something like Control.PointToScreen, using the x, y, coordinates based on the position of the control in its parent.

I am having toolstripDropDown and after clicking on toolstripDropDown button i wanted to show the context menu. So from above comments i modified my code in toolStripDropDown_Openining event as follows. it works fine.

void toolStripDropDownButton_DropDownOpening(object sender, EventArgs e)
    {


            ToolStripDropDownButton btnSender = (ToolStripDropDownButton)sender;
            Point ptLowerRight = new Point(btnSender.Bounds.Right, btnSender.Bounds.Bottom);
            ptLowerRight = PointToScreen(ptLowerRight);
            contextMenuStrip.Show(ptLowerRight);
    }
 contextMenuStrip1.Show(button1.PointToScreen(new Point(0, button1.Height)));

To show MenuStrip right under the button

Easy way

contextMenuStrip1.Show(Button1, Button1.PointToClient(Cursor.Position));

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top