Solved

.NET Forms Application System Tray Context Menu Not Disappearing

Posted on 2011-09-21
10
612 Views
Last Modified: 2013-12-17
I am writing a simple .NET Forms application that has a hidden window and shows a system tray icon (notifyIcon) that has a context menu.  When I right click it brings up the context menu but then the menu will not disappear until I select something on the list.  I want to make the context menu disappear if the user clicks on anything outside of the context menu (like another window or the taskbar or start menu or another system tray icon).  What is the quickest path to doing this?

So you can recreate the problem, I am attaching the function I use to handle the MouseDown event on the notifyIcon.

The problem is that I dont know what event (or events) to catch to trigger hiding the context menu.

Thanks in advance for your insight.
private void notifyIcon1_MouseDown_Test(object sender, MouseEventArgs e)
        {

            if (e.Button != MouseButtons.Right)
            {
                return;
            }

            //clear context menu
            contextMenuStrip1.Items.Clear();

            //set context menu items to the items
            ToolStripItem newContextMenuItem = contextMenuStrip1.Items.Add("Example Item");
            newContextMenuItem.Image = m_ContextMenuImages["started"];
            newContextMenuItem.Click += new EventHandler(ContextMenuSite_Click);

            contextMenuStrip1.Show(Cursor.Position);

        }

Open in new window

0
Comment
Question by:dave4dl
10 Comments
 
LVL 15

Expert Comment

by:Minh Võ Công
ID: 36572115
0
 
LVL 8

Accepted Solution

by:
jagrut_patel earned 400 total points
ID: 36572403
Either set-up the context-menu items at design-time or when your Form is Loaded. Once you associate Context Menu with the NotifyIcon there is no need to code the MouseDown event-handler at all. Context menu is designed to appear on right click.
0
 
LVL 15

Author Comment

by:dave4dl
ID: 36572416
That is what I am doing and that results in the behavior I describe (that I am looking for a fix for).
0
 
LVL 15

Author Comment

by:dave4dl
ID: 36572430
jagrut,

Thanks for the feedback.  I didnt say it explicitly but I am constructing the context menu each time the notifyIcon is clicked (so I need an event to do this construction before it is displayed).  How do i do this without using the mousedown event?
0
 
LVL 8

Expert Comment

by:jagrut_patel
ID: 36572449
If it is really needed to construct the context menu at run-time but only "once" then Form's Loaded event can be used.

Do you mean your context menu items are so dynamic that only last place you can decide what to show is when mouse-down happens?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 100 total points
ID: 36573762
Use the Opening() event of the ContextMenuStrip:
http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripdropdown.opening.aspx

You can set the items to be displayed from there.  I believe you can even cancel the event preventing the menu from appearing at all.
0
 
LVL 15

Author Comment

by:dave4dl
ID: 36574727
jagrut, I need to reconstruct the menu every time (this context menu shows all the IIS sites on the computer and the current state they are in) so doing it on form load will not work for me.
0
 
LVL 15

Author Comment

by:dave4dl
ID: 36574731
Idle_Mind, I will give that a shot and see if it works.
0
 
LVL 15

Author Comment

by:dave4dl
ID: 36574769
jagrut (and idle_mind) thank you for your solution on this, the key was using the built-in "ContextMenu" property of the notifyIcon instead of displaying it myself (as jagrut said).  Thanks!
0
 
LVL 15

Author Closing Comment

by:dave4dl
ID: 36574776
Thanks so much for your help!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now