Click here to Skip to main content
Licence CPOL
First Posted 13 Dec 2005
Views 166,992
Bookmarked 80 times

Notify Icon control in .NET 2.0 with balloon tips

By | 13 Dec 2005 | Article
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)

About the Author

kubben

Web Developer

United States United States

Member

I started my programmer career over 16 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 VS2010 and the ajax control toolkit.
 
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 Pinmemberraptor00-coolguy15:48 16 Dec '10  
GeneralError - .exe crashes when it goes to tray Pinmemberharalab21:57 15 Dec '09  
GeneralRe: Error - .exe crashes when it goes to tray Pinmemberkubben1:39 16 Dec '09  
GeneralRe: Error - .exe crashes when it goes to tray Pinmemberharalab3:12 16 Dec '09  
GeneralError: missing resources.resx Pinmemberkubben1:31 9 Dec '08  
QuestionNotify Icon Baloon Tip That takes more than 255 characters? PinmemberNigel Liefrink 221:51 21 Aug '08  
AnswerRe: Notify Icon Baloon Tip That takes more than 255 characters? Pinmemberkubben1:14 22 Aug '08  
QuestionApplication works with Citrix ! PinmemberMasygoli0:08 7 Jan '07  
AnswerRe: Application works with Citrix ! Pinmemberkubben2:18 7 Jan '07  
Dear Amitiee,
 
I am not sure I totally follow, but here is my guess as to what is going on. You have a distrubuted application that has some dll's that are on a server that remote user PC's use. I am guessing that the notify icon code ended up in these dll's. I am thinking that the code needs to be in the exe file, or the dll's need to be distributed to the local PC. I really haven't used citrix, so I don't know exactly what that adds to the issue.
 
You could always try my test application. Make sure it works when you run it on the local PC. Then put it on the server and see if it still works. If it doesn't work when you put it on the server the you probably have a rights issue because of the app not running in fully trusted mode.
 
If it ends up being a rights issue because of running the app from a server on the local PC I wrote an article about creating an MSI file that will fix the rights for that server.
http://www.codeproject.com/dotnet/Using_MSI_or_a_strong_nam.asp
 
I hope that helps.
Ben
GeneralRe: Application works with Citrix ! PinmemberMasygoli22:34 9 Jan '07  
GeneralRe: Application works with Citrix ! PinmemberMasygoli1:57 10 Jan '07  
GeneralRe: Application works with Citrix ! Pinmemberkubben2:29 10 Jan '07  
QuestionClick over the BalloonTip??? Pinmemberaxy1088:45 2 Aug '06  
AnswerRe: Click over the BalloonTip??? Pinmemberaxy1088:59 2 Aug '06  
QuestionRe: Click over the BalloonTip??? Pinmembertaranek1:47 23 Jul '07  
GeneralTImeout problem Pinmember2much4u21:11 20 Jul '06  
GeneralRe: TImeout problem Pinmemberkubben2:26 21 Jul '06  
GeneralRe: TImeout problem Pinmember2much4u23:45 22 Jul '06  
GeneralRe: TImeout problem Pinmembermoorej19993:07 9 Aug '07  
GeneralRe: TImeout problem Pinmemberkubben3:12 9 Aug '07  
Generalnotify Icon in asp,net PinmemberGERMAN REYES10:44 12 Jul '06  
GeneralRe: notify Icon in asp,net Pinmemberkubben2:19 14 Jul '06  
GeneralLogoff / Shutdown PinmemberGorillaman6:26 21 Mar '06  
GeneralRe: Logoff / Shutdown Pinmemberkubben8:12 21 Mar '06  
GeneralRe: Logoff / Shutdown PinmemberGorillaman8:22 21 Mar '06  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 13 Dec 2005
Article Copyright 2005 by kubben
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid