leora
leora

Reputation: 196459

how to show contextmenustrip when a button is clicked in the right position

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?

Upvotes: 23

Views: 62993

Answers (10)

sack sack
sack sack

Reputation: 1

The following solution can be applied for most control

contextMenuStrip1.Show(sender,sender.Location)

Upvotes: 0

Илья Апрол
Илья Апрол

Reputation: 21

There seems to be a right answer already, but I thought this might be a bit prettier:

private void button_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    menuStrip.Show(btn, new Point(0, 0)); // offset from the edge of your button
}

Upvotes: 2

Coder Tr25
Coder Tr25

Reputation: 19

 contextMenuStrip1.Show(button1.PointToScreen(new Point(0, button1.Height)));

To show MenuStrip right under the button

Upvotes: 1

saper_2
saper_2

Reputation: 81

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 :)

Upvotes: 7

Sagar
Sagar

Reputation: 429

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);
    }

Upvotes: 1

lllllllllllllIllllIll
lllllllllllllIllllIll

Reputation: 63

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);

Upvotes: 3

qursaan
qursaan

Reputation: 1

Easy way

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

Upvotes: -1

A J Qarshi
A J Qarshi

Reputation: 2992

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);
}

Upvotes: 51

leora
leora

Reputation: 196459

I figured it out:

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

Upvotes: 32

Michael Todd
Michael Todd

Reputation: 17041

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.

Upvotes: 1

Related Questions