A great way to insure that your program is easily accessible to the user is by putting an icon for your application in the system tray. I'll show you how to use the system tray with C# and the .NET Framework.
This blog post is also available in PDF format in a TechRepublic download, which includes a Visual Studio project file that contains all of the code for the functionality discussed.
How to use NotifyIconThe NotifyIcon object allows you to: place an icon for your program in the system tray, allow users to access a menu via that icon, and perform certain events for user actions such as mouse clicks. You can access the NotifyIcon object via the Toolbox in Visual Studio (see Figure A). Figure A
Simply drag the object onto the main form in your C# or .NET Framework application, and you will be able to access its properties and set the icon that you would like to display. To choose the icon you want to show in the taskbar, click the Choose Icon link in the Properties pane for NotifyIcon (see Figure B). Figure B
A dialog box will pop up that allows you to browse your computer and set the icon. After you set the icon, you're ready to wire up the form so that it uses the NotifyIcon component.
In my example, the goal is for the main form to display normally except when the user minimizes the form — then, I'll want to display an icon in the system tray. I'll also make the form invisible so that it does not show up in the taskbar.
When the NotifyIcon displays, the user will be able to double-click it to make the form visible again. Also, I am going to attach a context menu to the icon so the menu will display when the user right-clicks the icon.The first thing I will do is program the Minimize functionality. To do this, I subscribe to the form's Resize event. The code for this is shown below in Figure C. Figure C
This code checks to make sure that the form is minimized. If the user has minimized the form, you can make it invisible (this removes it from the taskbar) and show the NotifyIcon; if the user hasn't minimized the form, you don't want to take any action. The code also calls NotifyIcon.ShowBalloonTip, which causes a balloon tip to display above the icon.Now you need to give the user functionality so that the form will display again. You do this by subscribing to the NotifyIcon_DoubleClick event. Figure D shows the code to handle this event. Figure D
In this case, I placed the code in an external method; this allows you to use the code in other instances without having to duplicate it. This is a good idea when you're writing functionality for events, since event functionality is usually needed elsewhere in the program. The display code basically reverses the code I used in the Form_Resize event. I make the form visible, hide the NotifyIcon, set the form's WindowState to Normal, and I finally activate the form to ensure that it has focus. After these steps, the form will be ready for the user to interact with it.I'll also add a bit of functionality to the context menu that pops up when the user right-clicks the NotifyIcon. You must drag a ContextMenu component onto the form (see Figure E). Figure E
Once this component is on the form, click the NotifyIcon component and set its ContextMenu property equal to the name of your context menu.
At this point the context menu is set up and ready to use. For this article, I will add two small bits of functionality to the menu. The first is the ability to show the main form to the user similarly to how I displayed it on a double-click. The second is to give the user an option to exit out of the application. You will need to add two menu items to the context menu to do this — name one showForm and the other exitApplication.To wire up the functionality for these two menu items, simply subscribe to their MenuItem_Click events. The code to place in the events is shown below in Figure F and Figure G. Figure F
The code for these events is very straightforward; however, there is no reason you cannot use the context menu for tasks that aren't as trivial. Also notice that the Display method is reused.
Uses for NotifyIcon
You can use the NotifyIcon any time you expect users to either keep your application open for extended periods of time and they won't be directly interacting with the application on a regular basis. That is why many virus scanners, video drivers, and network applications have icons in the system tray.
You can use the system tray for other application types but be careful not to overuse it. If used incorrectly it can become more of a hindrance than a help, and many users may be confused if it is used incorrectly.