?
Solved

.NET Forms Application System Tray Context Menu Not Disappearing

Posted on 2011-09-21
10
Medium Priority
?
639 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 1600 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

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

Python: Series & Data Frames With Pandas

Learn the basics of Python’s pandas library of series & data frames and how we can use these tools for data manipulation.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

762 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