Solved

.NET Forms Application System Tray Context Menu Not Disappearing

Posted on 2011-09-21
10
618 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

726 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