Monday, February 22, 2010

Displaying context menu for system tray icon in C#

In the last post, I talked about creating a system tray icon for applications. Often, there is also a need to show menus when the user clicks this icon. Let’s see how we can accomplish this.

To enable menus on the system tray icon, we need to use the ContextMenuStrip control. Drop this control from the toolbox onto the form design surface. The control will be displayed in the bottom half, below the actual form. We need to do 3 things now to make the menu work:

  • Link the menu to the icon – The ContextMenuStrip only represents a menu of options. To make it work, we need to link it to another control, in our case, it is the NotifyIcon control. Select the NotifyIcon control, go to its Properties and set the ContextMenuStrip property to point to the menu control.
  • Add the menu options – Select the menu control and go to its properties. For the Items property, click on the Ellipsis to open the Items Collection Editor. Here, we can add the menu items we want to appear in the context menu
  • Add event handlers for menu options – Finally, we need to specify what should happen when the menu items are clicked. For this, we need to attach event handlers. Attaching event handlers is pretty straightforward. In the code behind, use the normal <controlName>.<eventName> += <eventHandler> syntax to specify the event handler. For example, for a control named menuExit, we can attach menuExit_Click as a Click event handler using the following line:

menuExit.Click += new EventHandler(menuExit_Click);

You can add as many menu options as you want. In fact, it is quite common to have applications like download accelerators and anti virus software to run in the background with an icon in the system tray that has huge (often multi level) menus. But now, creating such menus is no longer limited to large product companies. You also know the trick for making menus like these :-)

Monday, February 15, 2010

Difference in Repeat behavior between Windows Media Player and Winamp

Most of you would have used at the Windows media player (WMP) or Winamp or both as your music player of choice. Both of them provide 2 options to control the sequence in which songs are played. These are Shuffle and Repeat. The definition of Shuffle is the same in both the players and basically means that instead of the songs being played sequentially, they will be chosen randomly from the playlist. However, there is a subtle difference in the definition of Repeat. Let me try to highlight this difference in this post.

There are 4 combinations possible between Shuffle and Repeat. The table below highlights the behavior of each player for the various combinations.

Shuffle/Repeat combination

Windows Media Player

Winamp

Shuffle – OFF
Repeat – OFF
Plays songs in sequence. Cycles to the beginning of the list once the last song is played Plays songs in sequence. Stops playing songs once the last song is played
Shuffle – OFF
Repeat – ON
Plays songs in sequence. Cycles to the beginning of the list once the last song is played Plays songs in sequence. Cycles to the beginning of the list once the last song is played
Shuffle – ON
Repeat – OFF
Plays songs randomly. Cycles to the beginning of the list once the last song is played Plays songs randomly and stops when each song in the playlist has been played once
Shuffle – ON
Repeat – ON
Plays songs randomly. Songs can be repeated even when all the songs in the playlist have not yet been played at least once Plays songs randomly. Songs can be repeated even when all the songs in the playlist have not yet been played at least once

 

As is evident from the above table, the difference in the behaviors of both the players comes when Repeat is OFF. This is because of 2 reasons:

  • Repeat in Winamp means turning repeat of song and playlist on/off
  • Repeat in WMP only means turning repeat of songs on or off. Repeat of playlist is not under the control of the user. Once the playlist is complete, WMP will automatically cycle back to the beginning of the list.

Given this understanding of Repeat, when using Shuffle in WMP, based on the playlist type, we should set the Repeat as on/off as follows:

  • Playlist with few songs : We might want to set Repeat OFF so that all the songs in the playlist have an equal chance of being played. No song will be repeated till ALL songs have been played once
  • Playlist with large number of songs : We might want to set Repeat ON otherwise we might have to wait a long time before a song we hear once gets played again

Hope this post helps in clarifying the difference in behavior of both these popular players and aids you in choosing the right setting when listening to your favorite songs.

Thursday, February 4, 2010

Creating a system tray application in C#

Many a times, we want to create applications that sit in the system tray (the area where the clock, volume control etc are displayed) instead of/in addition to the taskbar. Or, when the user minimizes the application window, we would want it to work in the background and display an icon for it in the system tray. Doing this is very easy and can be accomplished by the following steps:

  1. Hiding the form
  2. Turn OFF the taskbar display
  3. Setting properties for display in system tray
  • Hiding the form – This is pretty straightforward and needs just 2 lines of C# code
   1: this.WindowState = FormWindowState.Minimized;


   2: this.Hide();






where “this” refers to the form being hidden




  • Set taskbar display to be OFF – This needs setting just a single property which can be set either at design time or at run time, as follows





   1: this.ShowInTaskbar = false;




again, “this” refers to the form being hidden




  • Setting properties for display in system tray – To display the application icon in the system tray, we need to drop the NotifyIcon control from the toolbox onto the form. The control will be displayed under the actual form on the design surface. After dropping the control, we can set the following properties for it:



    • Text – This is the text that will be displayed when you hover the mouse over the system tray icon


    • Icon – An "ico” icon file which will be used as the icon in the system tray


    • BalloonTipText – The text which will be displayed when you enable the balloon tip


    • BalloonTipTitle – The title of the balloon tip when it is enabled




So there you have it, 3 simple steps to enable system tray display for your application.