Solved

.NET Forms Application System Tray Context Menu Not Disappearing

Posted on 2011-09-21
10
623 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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
 
LVL 86

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 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