locked
Disabling Context Menu from being displayed? RRS feed

  • Question

  •  

    I have context Menu which I display on a form using the following code:

     

    myContextMenu.Show(this, 0,0);

     

    Want I want to do is, if the user does not use the context menu within 1 minute the context menu shall disappear and the screen shall revert to the one before the context menu was displayed (I'm using GDI+).

     

    I can't figure out how to stop displaying the context menu, would anyone have any have suggestions?

     

    I've tried:

     

    myContextMenu.Visible = false;

    Refresh();

     

    although the context menu still is there?


    Thanks in advance for your help!

    Monday, June 9, 2008 4:07 PM

Answers

  • How are you calling it.  I put together this simple code consisting of one button and one context menu, and it seems to work fine:

    using System;  
    using System.ComponentModel;  
    using System.Threading;  
    using System.Windows.Forms;  
     
    namespace WindowsApplication2  
    {  
        public partial class Form1 : Form  
        {  
            public Form1()  
            {  
                InitializeComponent();  
     
            }  
     
            void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)  
            {  
                contextMenuStrip1.Visible = false;  
            }  
     
            void worker_DoWork(object sender, DoWorkEventArgs e)  
            {  
                Thread.Sleep(3000);  
            }  
     
            private void button1_Click(object sender, EventArgs e)  
            {  
                contextMenuStrip1.Show(this, 0, 0);  
                BackgroundWorker worker = new BackgroundWorker();  
                worker.DoWork += worker_DoWork;  
                worker.RunWorkerCompleted += worker_RunWorkerCompleted;  
                worker.RunWorkerAsync();  
            }  
        }  

    David Morton - Consultant - Catapult Systems - Houston
    Monday, June 9, 2008 4:15 PM
  •  
    ContextMenu ctx = new ContextMenu ( );  
    ctx.Collapse ( ); 

    AlexB
    Monday, June 9, 2008 4:15 PM

All replies

  • How are you calling it.  I put together this simple code consisting of one button and one context menu, and it seems to work fine:

    using System;  
    using System.ComponentModel;  
    using System.Threading;  
    using System.Windows.Forms;  
     
    namespace WindowsApplication2  
    {  
        public partial class Form1 : Form  
        {  
            public Form1()  
            {  
                InitializeComponent();  
     
            }  
     
            void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)  
            {  
                contextMenuStrip1.Visible = false;  
            }  
     
            void worker_DoWork(object sender, DoWorkEventArgs e)  
            {  
                Thread.Sleep(3000);  
            }  
     
            private void button1_Click(object sender, EventArgs e)  
            {  
                contextMenuStrip1.Show(this, 0, 0);  
                BackgroundWorker worker = new BackgroundWorker();  
                worker.DoWork += worker_DoWork;  
                worker.RunWorkerCompleted += worker_RunWorkerCompleted;  
                worker.RunWorkerAsync();  
            }  
        }  

    David Morton - Consultant - Catapult Systems - Houston
    Monday, June 9, 2008 4:15 PM
  •  
    ContextMenu ctx = new ContextMenu ( );  
    ctx.Collapse ( ); 

    AlexB
    Monday, June 9, 2008 4:15 PM
  • David, I think he is doing ContextMenu not ContextMenuStrip.
    AlexB
    Monday, June 9, 2008 4:17 PM
  • Thanks so much for your help!

    I'm using the ContextMenuStrip, however when I use contextMenuStrip1.Visible = 
    false
    ; it doesn't seem to work.  It just shows the GDI+ interface behind the  ContextMenuStrip.

    Also, I have another question can I add pictures to the ContextMenuStrip menu items on the right side, I only seem to add them to the left side?

    Monday, June 9, 2008 6:57 PM
  •  

    contextMenuStrip1.Close();


    I need a job!
    Monday, June 9, 2008 7:07 PM
  • Many thanks cablehead.

    Would anyone know about addign an image to right side of the context menu?
    Monday, June 9, 2008 7:12 PM
  • I'm taliking about something like this, however I want the image in the right side?  http://z.about.com/d/visualbasic/1/0/C/A/StripOpts.gif
    • Proposed as answer by robben07160 Monday, June 9, 2008 7:23 PM
    • Unproposed as answer by robben07160 Monday, June 9, 2008 7:23 PM
    Monday, June 9, 2008 7:15 PM
  •  

    contextMenuStrip1.Items[0].ImageAlign = ContentAlignment.MiddleRight;


    I need a job!
    • Proposed as answer by robben07160 Monday, June 9, 2008 7:23 PM
    Monday, June 9, 2008 7:21 PM
  •  For questions and discussions regarding client application development using Windows Forms controls and features, please see http://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=8&SiteID=1

    http://www.peterRitchie.com/blog
    Monday, June 9, 2008 7:53 PM
  •  Ok I have tried everything, I can't still seem to close the contextmenustrip, it seems to work when I click on the menu options i.e. "Exit".

    However, the timeout part (this is where if the menu has not been used for a specific amount of time it will close the context menu). The  timeout code is the same as the code if I click on the "Exit" option on the menu. So i think it something  to with the timer function. I'm using a timer which calls an event which then checks to see if the specific amount of time has passed in order to stop displaying the contextmenu. However the menu doesn't seem to go. Could anyone tell me want they think could be the problem?

    Also, I can't seem to get the image on the right side. I'm using contextMenuStrip1.Items[0].ImageAlign = ContentAlignment.MiddleRight and I also have selected the  option from the control but I still can't get it working it just seems to display the image for each menu option on the left?

    Thanks in advance for your help!
    Tuesday, June 10, 2008 10:35 AM
  •  Also, I can't seem to get the image on the right side. I'm using contextMenuStrip1.Items[0].ImageAlign = ContentAlignment.MiddleRight and I also have selected the  option from the control but I still can't get it working it just seems to display the image for each menu option on the left?

    There is a chance you will have to write your own custom control.

    However, let me ask you a question. Do you assign the image property to the right (contextMenuStrip1.Items[0].ImageAlign = ContentAlignment.MiddleRight) in the constructor or somewhere in the code after the fact?
    AlexB
    Tuesday, June 10, 2008 12:50 PM
  • It is defined in the InitializeComponent function, is that correct?
    Tuesday, June 10, 2008 2:27 PM