Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C#
Expertise: Intermediate
Dec 12, 2005

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}");
}
Boris Eligulashvili
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap