Friday, December 9

Notify Icon control in .NET 2.0 with balloon tips

Notify Icon control in .NET 2.0 with balloon tips

Sample Image - NotifyIconControl20.gif


In previous versions of .NET, if you wanted to have a notify icon in the tray, you had to use the notify icon class and do a little extra work. Now, in .NET 2.0, there is a notify icon control that you can drop on to your Windows form. One nice thing that has been added is the balloon tip properties.


I had just written an article on using Windows messaging to cause an existing instance of an application to show up even if it was minimized or hidden, like in a tray icon. So, I was doing a little investigation to see what was different in .NET 2.0. That�s when I saw the notify icon control and decided to play with it. I don�t currently have any application in production that uses this code, but I think sometime soon I probably will.

The Problem

You may ask, why use a notify icon at all? I guess the answer that comes to mind is so that you can see your application in the tray. In my example, when the application is minimized, I hide the form from the task bar.
Note: There is a new property on the form that you can use if you don�t ever want the form to show up in the task bar. It is called �ShowInTaskBar�. If you set this property to false, the app will not show up in the task bar.
This app hides the form from the task bar on minimize and shows it when it is not minimized. A problem with using the tray is users can get confused and not know where the application went. Especially, when you hide the form from the task bar like this app is doing. So, the solution here is to use the balloon tip. It is a nice feature that allows you to display a little balloon message above the tray icon when certain things happen.

The Solution

This app displays this balloon tip message when you minimize the form.
Balloon tip on minimize of the form
Next, this app displays this balloon tip message when you click the X in the upper right hand corner. This is similar to what the Windows Messenger does.
Note: I am not a big fan of overwriting the click on the x in the upper right hand corner from its default functionality which is to close the app, but I thought I would do it in this app for an example.
This balloon tip shows when the user clicks the X in the upper right hand corner

The Code

Here is the code that checks the current Windows state so we know if we should hide or show the form on the task bar:
private void Form1_Move(object sender, EventArgs e)
  //This code causes the form to not show up on the task bar only in the tray.

  //NOTE there is now a form property that will allow you to keep the 

  //application from every showing up in the task bar.

  if (this == null)
  { //This happen on create.

  //If we are minimizing the form then hide it so it doesn't show up on the 

  //task bar

  if (this.WindowState == FormWindowState.Minimized)
    notifyIcon1.ShowBalloonTip(3000, "Test App",
            "The App has be moved to the tray.",
  {//any other windows state show it.


Here is the code that checks when the form is closing to see if we will really close the app or just send it to the tray:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  //There are several ways to close an application.

  //We are trying to find the click of the X in the upper right hand corner

  //We will only allow the closing of this app if it is minimized.

  if (this.WindowState != FormWindowState.Minimized)
    //we don't close the app...

    e.Cancel = true;
    //minimize the app and then display a message to the user so

    //they understand they didn't close the app they just sent it to the tray.

    this.WindowState = FormWindowState.Minimized;
    //Show the message.

    notifyIcon1.ShowBalloonTip(3000, "Test App",
         "You have not closed this appliation."+
         (Char)(13)+"It has be moved to the tray."+
         (Char)(13)+"Right click the Icon to exit.",
Here is the code which runs when you double click the tray icon to bring it back to the normal Windows state:
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
  if (this.WindowState == FormWindowState.Minimized)
    this.WindowState = FormWindowState.Normal;

  // Activate the form.



No comments:

Post a Comment

Please don't spam, spam comments is not allowed here.

shibashish mohanty