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

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist