Click here to Skip to main content
Click here to Skip to main content

Notify Icon control in .NET 2.0 with balloon tips

, 13 Dec 2005
Rate this:
Please Sign up or sign in to vote.
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:

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:

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:

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)

Share

About the Author

kubben
Web Developer
United States United States
I started my programmer career over 18 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 both vb.net and C#. Lately I have been using VS2012 and writing a Windows 8 app. You can search for the app it is called ConvertIT.
 
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 PinmemberXiao Lan29-Jul-13 3:49 
AnswerRe: How to get the close button in the balloon notification Pinmemberkubben29-Jul-13 5:34 
Questionhow Don't hide the Notify Balloon of tray app? PinmemberMember 946437113-Jun-13 23:57 
AnswerRe: how Don't hide the Notify Balloon of tray app? Pinmemberkubben14-Jun-13 1:54 
QuestionHyperlinks on Balloon Tips PinmemberS.M. Zamshed Farhan26-Jan-13 17:59 
AnswerRe: Hyperlinks on Balloon Tips PinmemberZamshed Farhan9-Feb-13 19:19 
Can anyone help me about this matter please?
GeneralRe: Hyperlinks on Balloon Tips Pinmemberkubben10-Feb-13 1:57 
GeneralRe: Hyperlinks on Balloon Tips PinmemberZamshed Farhan10-Feb-13 15:51 
GeneralMy vote of 5 Pinmembercngirwa6-Jan-13 2:06 
GeneralRe: My vote of 5 PinmemberS.M. Zamshed Farhan22-Jan-13 17:31 
GeneralMy vote of 1 Pinmemberraptor00-coolguy16-Dec-10 15:48 
GeneralRe: My vote of 1 PinmemberS.M. Zamshed Farhan22-Jan-13 20:58 
GeneralError - .exe crashes when it goes to tray Pinmemberharalab15-Dec-09 21:57 
GeneralRe: Error - .exe crashes when it goes to tray Pinmemberkubben16-Dec-09 1:39 
GeneralRe: Error - .exe crashes when it goes to tray Pinmemberharalab16-Dec-09 3:12 
GeneralError: missing resources.resx Pinmemberkubben9-Dec-08 1:31 
GeneralRe: Error: missing resources.resx PinmemberS.M. Zamshed Farhan22-Jan-13 18:13 
GeneralRe: Error: missing resources.resx Pinmemberkubben23-Jan-13 1:43 
GeneralRe: Error: missing resources.resx PinmemberS.M. Zamshed Farhan23-Jan-13 16:20 
GeneralRe: Error: missing resources.resx Pinmemberkubben23-Jan-13 16:22 
QuestionNotify Icon Baloon Tip That takes more than 255 characters? PinmemberNigel Liefrink 221-Aug-08 21:51 
AnswerRe: Notify Icon Baloon Tip That takes more than 255 characters? Pinmemberkubben22-Aug-08 1:14 
QuestionApplication works with Citrix ! PinmemberMasygoli7-Jan-07 0:08 
AnswerRe: Application works with Citrix ! Pinmemberkubben7-Jan-07 2:18 
GeneralRe: Application works with Citrix ! PinmemberMasygoli9-Jan-07 22:34 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 13 Dec 2005
Article Copyright 2005 by kubben
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid