Click here to Skip to main content
15,861,125 members
Articles / Programming Languages / C#
Article

Notify Icon control in .NET 2.0 with balloon tips

Rate me:
Please Sign up or sign in to vote.
4.37/5 (25 votes)
13 Dec 2005CPOL3 min read 285.5K   4.9K   85   59
This article shows how to use the new notify icon control along with the new balloon tip properties.

Sample Image - NotifyIconControl20.gif

Introduction

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.

Background

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:

C#
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.
    return;
  }
  //If we are minimizing the form then hide it so it doesn't show up on the 
  //task bar
  if (this.WindowState == FormWindowState.Minimized)
  {
    this.Hide();
    notifyIcon1.ShowBalloonTip(3000, "Test App",
            "The App has be moved to the tray.",
            ToolTipIcon.Info);
  }
  else
  {//any other windows state show it.
    this.Show();
  }

}

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:

C#
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.",
         ToolTipIcon.Info);
  }
}

Here is the code which runs when you double click the tray icon to bring it back to the normal Windows state:

C#
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
  if (this.WindowState == FormWindowState.Minimized)
  {
    this.Show();
    this.WindowState = FormWindowState.Normal;
  }

  // Activate the form.
  this.Activate();
  this.Focus();
}

Conclusion

Hopefully, this article showed you how to use the new notify icon control and the balloon tip that comes with it. I hope you find this information useful.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
I started my programmer career over 26 years ago doing COBOL and SAS on a MVS mainframe. It didn't take long for me to move into windows programming. I started my windows programming in Delphi (Pascal) with a Microsoft SQL server back end. I started working with vb.net when the beta 2 came out in 2001. After spending most of my programming life as a windows programmer I started to check out asp.net in 2004. I achieved my MCSD.net in April 2005. I have done a lot of MS SQL database stuff. I have a lot of experience with Window Service and Web services as well. I spent three years as a consultant programing in C#. I really enjoyed it and found the switch between vb.net and C# to be mostly syntax. In my current position I am programming in C# working on WPF and MSSql database stuff. Lately I have been using VS2019.

On a personal note I am a born again Christian, if anyone has any questions about what it means to have a right relationship with God or if you have questions about who Jesus Christ is, send me an e-mail. ben.kubicek[at]netzero[dot]com You need to replace the [at] with @ and [dot] with . for the email to work. My relationship with God gives purpose and meaning to my life.

Comments and Discussions

 
QuestionHow to get the close button in the balloon notification Pin
Xiao Lan29-Jul-13 3:49
Xiao Lan29-Jul-13 3:49 
AnswerRe: How to get the close button in the balloon notification Pin
kubben29-Jul-13 5:34
kubben29-Jul-13 5:34 
Questionhow Don't hide the Notify Balloon of tray app? Pin
Ankit Kumar Bansal13-Jun-13 23:57
professionalAnkit Kumar Bansal13-Jun-13 23:57 
AnswerRe: how Don't hide the Notify Balloon of tray app? Pin
kubben14-Jun-13 1:54
kubben14-Jun-13 1:54 
QuestionHyperlinks on Balloon Tips Pin
Zamshed Farhan26-Jan-13 17:59
Zamshed Farhan26-Jan-13 17:59 
AnswerRe: Hyperlinks on Balloon Tips Pin
Zamshed Farhan9-Feb-13 19:19
Zamshed Farhan9-Feb-13 19:19 
GeneralRe: Hyperlinks on Balloon Tips Pin
kubben10-Feb-13 1:57
kubben10-Feb-13 1:57 
GeneralRe: Hyperlinks on Balloon Tips Pin
Zamshed Farhan10-Feb-13 15:51
Zamshed Farhan10-Feb-13 15:51 
GeneralMy vote of 5 Pin
cngirwa6-Jan-13 2:06
cngirwa6-Jan-13 2:06 
GeneralRe: My vote of 5 Pin
Zamshed Farhan22-Jan-13 17:31
Zamshed Farhan22-Jan-13 17:31 
GeneralMy vote of 1 Pin
raptor00-coolguy16-Dec-10 15:48
raptor00-coolguy16-Dec-10 15:48 
GeneralRe: My vote of 1 Pin
Zamshed Farhan22-Jan-13 20:58
Zamshed Farhan22-Jan-13 20:58 
GeneralError - .exe crashes when it goes to tray Pin
haralab15-Dec-09 21:57
haralab15-Dec-09 21:57 
GeneralRe: Error - .exe crashes when it goes to tray Pin
kubben16-Dec-09 1:39
kubben16-Dec-09 1:39 
I have used this on XP without issues. I have not yet tried this on Windows 7 so I am not sure if things have changed which would cause it not to work.

Ben
GeneralRe: Error - .exe crashes when it goes to tray Pin
haralab16-Dec-09 3:12
haralab16-Dec-09 3:12 
GeneralError: missing resources.resx Pin
kubben9-Dec-08 1:31
kubben9-Dec-08 1:31 
GeneralRe: Error: missing resources.resx Pin
Zamshed Farhan22-Jan-13 18:13
Zamshed Farhan22-Jan-13 18:13 
GeneralRe: Error: missing resources.resx Pin
kubben23-Jan-13 1:43
kubben23-Jan-13 1:43 
GeneralRe: Error: missing resources.resx Pin
Zamshed Farhan23-Jan-13 16:20
Zamshed Farhan23-Jan-13 16:20 
GeneralRe: Error: missing resources.resx Pin
kubben23-Jan-13 16:22
kubben23-Jan-13 16:22 
QuestionNotify Icon Baloon Tip That takes more than 255 characters? Pin
Nigel Liefrink 221-Aug-08 21:51
Nigel Liefrink 221-Aug-08 21:51 
AnswerRe: Notify Icon Baloon Tip That takes more than 255 characters? Pin
kubben22-Aug-08 1:14
kubben22-Aug-08 1:14 
QuestionApplication works with Citrix ! Pin
Masygoli7-Jan-07 0:08
Masygoli7-Jan-07 0:08 
AnswerRe: Application works with Citrix ! Pin
kubben7-Jan-07 2:18
kubben7-Jan-07 2:18 
GeneralRe: Application works with Citrix ! Pin
Masygoli9-Jan-07 22:34
Masygoli9-Jan-07 22:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.