Solved

.NET Forms Application System Tray Context Menu Not Disappearing

Posted on 2011-09-21
10
614 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

825 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