devxlogo

How to Hide a ContextMenu

How to Hide a ContextMenu

To display the shortcut menu, you call ContextMenu.Show() method. Typically, you call this method if the right mouse button on a control or area of the form is clicked. The following snippet demonstrates this:

private void myControl_MouseUp(object sender,                  System.Windows.Forms.MouseEventArgs e){    if (e.Button == MouseButtons.Right)        myControl.ContextMenu.Show(myControl,            new Point(e.X, e.Y) );      }

After an item on the ContextMenu is selected and clicked, the ContextMenu, generally, should be hidden. It will happen “automatically” if you display another GUI component in response to this click.

In some cases, when you are not updating the screen, the ContextMenu stays on the screen after the click. In order to hide the ContextMenu, call the SendKeys.Send() method and pass the {ESC} code, as follows:

private void menuItemDoSomething_Click(object sender,                   System.EventArgs e){    //Your Code are here    SendKeys.Send("{ESC}");}

devx-admin

Share the Post: