locked
ContextMenuStrip questions RRS feed

  • Question

  • Hello everyone,
    how can I in C# find out the location where a contextmenustrip appeared, I am trying to trap the opened or opening event to get some location data but nothing. and once I do have that, can I through code open the context menu in the same location?
    Monday, October 19, 2009 6:44 PM

Answers

  • You can get the location of contextmenu at opening event by using Left and top properties
            private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
            {
                this.Text = "Left:" + contextMenuStrip1.Left.ToString() + " Top:" + contextMenuStrip1.Top.ToString();
            }

    And you can open context menu with the code below.
    In this both examples x and y coordinates are referenced to the top left corner of screen not form or any other control.

    contextMenuStrip1.Show(new Point(10, 10));
    • Proposed as answer by Tamer Oz Thursday, October 22, 2009 6:24 PM
    • Marked as answer by eryang Wednesday, October 28, 2009 2:43 AM
    Monday, October 19, 2009 7:08 PM
  • That's what the Show(Control, Point) overload is meant to do.  Or you can do it yourself with Control.PointToScreen() on
    Hans Passant.
    • Marked as answer by eryang Wednesday, October 28, 2009 2:43 AM
    Monday, October 19, 2009 8:50 PM

All replies

  • You can get the location of contextmenu at opening event by using Left and top properties
            private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
            {
                this.Text = "Left:" + contextMenuStrip1.Left.ToString() + " Top:" + contextMenuStrip1.Top.ToString();
            }

    And you can open context menu with the code below.
    In this both examples x and y coordinates are referenced to the top left corner of screen not form or any other control.

    contextMenuStrip1.Show(new Point(10, 10));
    • Proposed as answer by Tamer Oz Thursday, October 22, 2009 6:24 PM
    • Marked as answer by eryang Wednesday, October 28, 2009 2:43 AM
    Monday, October 19, 2009 7:08 PM
  • hi Tamer,
    " In this both examples x and y coordinates are referenced to the top left corner of screen not form or any other control. "
    Is there any way to have that point be the point in reference to the application it resides in?
    Monday, October 19, 2009 8:39 PM
  • That's what the Show(Control, Point) overload is meant to do.  Or you can do it yourself with Control.PointToScreen() on
    Hans Passant.
    • Marked as answer by eryang Wednesday, October 28, 2009 2:43 AM
    Monday, October 19, 2009 8:50 PM
  • but I still use the contextMenuStrip1.Left and contextMenuStrip1.Top and they will point relative to applcaition, not Screen?
    Monday, October 19, 2009 8:55 PM
  • That's what the Show(Control, Point) overload is meant to do.  Or you can do it yourself with Control.PointToScreen() on
    Hans Passant.


    I just wanted to clarify my question :
    I will go through my application, and at one point capture where a contextmenustrip was loaded like this :
    int x = contextMenuStrip1.Left
    int y = contextMenuStrip1.Top

    Now at another point I want to show the same contextmenu in the same place it was recorded like so :
    When calling ContextMenuStrip1.Show(New Point (x, Y));
    how can I make sure that the show will be relevant to the application it was in, not the screen?
    Monday, October 19, 2009 9:32 PM
  • Another issue here is that the show method is only available for .NET 3.5 and up!!!, does anyone know how can I show a contextmenu via code in .NET 2.0?
    Tuesday, October 27, 2009 8:51 PM
  • No, it's been around since 2.0

    Hans Passant.
    Tuesday, October 27, 2009 9:42 PM