Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to create a button which will show menu items(Context Menu) after clicking(Normal Left Click) it....?

I have created a context menu and trying to show that on button click.
It is showing but can not set the proper location of context menu.

Here is Code :
C#
private void button1_Click(object sender, EventArgs e)
{
    contextMenuStrip1.Visible = true;
    contextMenuStrip1.Left = button1.Left;
    contextMenuStrip1.Top = button1.Top - contextMenuStrip1.Height;
}



I want context menu should be display above the button after clicking button.

Please HELP !!!
Posted

1 solution

I think you are looking for this:
SplitButton: an XP style dropdown split button[^]

------

Ok so try this:
contextMenuStrip1.Show(MousePosition);


------

Then do:
Point pt = PointToScreen(button1.Location);
//or this one
//Point pt = PointToScreen(new Point(button1.Right, button1.Top));
contextMenuStrip1.Show(pt);
 
Share this answer
 
v3
Comments
Prasad131 31-Mar-11 6:13am    
It can be helpful to me but I cannot run that project on my pc...!
It is giving assembly error...!

Plz Help !!!
Olivier Levrey 31-Mar-11 6:17am    
I updated my answer.
Prasad131 31-Mar-11 6:24am    
Dear Olivier,
contextMenuStrip1.Show(MousePosition);
is showing contextmenustrip at mouse position.
But i want it at fix position (Exactly Above the Button1).

Please Do Reply !
Olivier Levrey 31-Mar-11 6:43am    
I updated my answer one more time.
Prasad131 31-Mar-11 7:20am    
Thanks Olivier !!!
I have just added this additional line & It works exactly as i needed.

Point pt = PointToScreen(button1.Location);
pt.Y = pt.Y - contextMenuStrip1.Height;
contextMenuStrip1.Show(pt);

Thank You So Much !!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900