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

Simple class for easily minimizing to tray and adding context menu with ONE line of code

, 1 Jul 2005
Rate this:
Please Sign up or sign in to vote.
This class allows the user to implement minimizing to tray in one line of code.

Introduction

I usually like to implement the minimizing to the system tray feature in my applications... In addition, I also like to add a context menu to the icon which allows the user to restore the app from the tray or exit. After adding the same several lines of code to each application, I realized that it would be beneficial to streamline the process.

The Class

This class allows you to add this functionality in one line of code. The constructor takes the main form (Me) of your application, the text to add to the icon [optional], and the icon you want to display in the tray [optional - defaults to application icon].

Example:

Dim minToTray As New ClsMinToTray(Me, "MyApp is running...", myIcon)

Options: You can also choose the window state on restore and add to the context menu (the default tray icon menu has two items - Restore and Close).

Example (choose window state):

minToTray.restoreWindowState = FormWindowState.Maximized

Example (add to default context menu):

Private Sub Form1_Load(ByVal sender As Object, _
             ByVal e As System.EventArgs) Handles MyBase.Load
  minToTray.trayMenu.MenuItems.Add("New Menu Item", _
                          AddressOf NewMenuItem_click)
End Sub

Public Sub NewMenuItem_click(ByVal sender As Object, ByVal e As EventArgs)
  'Code Here
End Sub

Notes

This class seems to work well for me. I have used it in both VS2003 and VS2005b applications. The only issue I have ran into is in VS2005b. The ShowInTaskbar property of the main form (on multi-container) application seems to cause some anchoring problems (forms layout is affected on restore). I'm assuming this is a VS2005b bug...or maybe I'm missing something.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

jchrisrenfrow

United States United States
29, but fairly new to development. Graduating from the University of South Florida this summer (2005). I have strong interest in VB.NET, ASP/ASP.NET programming. My experience is in database design; therefore I find it fun to put frontend interfaces in both client and client/server database applications.
**Will be looking for employment soon Smile | :)

Comments and Discussions

 
GeneralThis is pretty good! PinmemberAnt210021-Feb-10 3:03 
GeneralExcellent code!! PinmemberLee Green23-Oct-07 10:18 
GeneralWorks Flawlessly! PinmemberLeerz27-Oct-06 4:29 
GeneralTrayText Property PinmemberFluid Dynamics Software23-Feb-06 6:09 
QuestionCan't get it to work... Pinmembersteveengland9-Sep-05 5:21 
GeneralRestore and AxMediaPlayer PinmemberJLCarbwood16-Aug-05 17:34 
GeneralRe: Restore and AxMediaPlayer Pinmemberjchrisrenfrow16-Aug-05 17:49 
GeneralUrgent Help Needed :context menu does not appear PinmemberGul Hunerkar13-Jul-05 4:17 
Hi, I am writing a c# code that opens a file and searches for some condition and according to the condition that occuring tray icon changes.This should be done every 10 minutes.
 
I have coded this part well but I can not exit my application from a context menu item(exit) which never appears.
 
If I erase the line where my while loop appear, context menu appears and every thing goes well.
 
Do you have any suggestion where I might have made a mistake? I am a c# newbie and I cannot find the solution...
 
thanks for your help very much..
 

Here is the code:
 
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Diagnostics;
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.ComponentModel.IContainer components;
private System.Boolean check;
 
public Form1()
{
this.check=true;
//this.HideApp();
//this.ShowInTaskbar = false;
this.components = new System.ComponentModel.Container();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
// Initialize contextMenu1
this.contextMenu1.MenuItems.AddRange(
new System.Windows.Forms.MenuItem[] {this.menuItem1});
 
// Initialize menuItem1
this.menuItem1.Index = 0;
this.menuItem1.Text = "E&xit";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
 
// Set up how the form should be displayed.
//this.ClientSize = new System.Drawing.Size(292, 266);
 
this.Text = "Notify Icon Example";
 
// Create the NotifyIcon.
 
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
notifyIcon1.ContextMenu = this.contextMenu1;
 
// The Text property sets the text that will be displayed,
// in a tooltip, when the mouse hovers over the systray icon.
notifyIcon1.Text = "Form1 (Situation controller)";
notifyIcon1.Visible = true;
notifyIcon1.Icon = new Icon("Icon2.ico");
while(this.check==true)
{
FileStream file = new FileStream("path.txt", FileMode.Open, FileAccess.Read,FileShare.Read);
StreamReader sr = new StreamReader(file);
string s;
bool control=false;
while((s = sr.ReadLine())!= null)
{
if(s.IndexOf("down")!=-1)
{
control=true;
}
}
if(control==true)
{
notifyIcon1.Icon = new Icon("Icon2.ico");
}
else
{
notifyIcon1.Icon = new Icon("Icon1.ico");
}
sr.Close();
file.Close();
//bu kısmı degistirdim
Thread.Sleep(1000*10*1);//1000*60*10
//this.Close();
//Application.Exit();
}
this.notifyIcon1.Dispose();
}
 
protected override void Dispose( bool disposing )
{
// Clean up any components being used.
if( disposing )
if (components != null)
components.Dispose();
 
base.Dispose( disposing );
}
 
private void notifyIcon1_DoubleClick(object Sender, EventArgs e)
{
// Show the form when the user double clicks on the notify icon.
 
// Set the WindowState to normal if the form is minimized.
if (this.WindowState == FormWindowState.Minimized)
this.WindowState = FormWindowState.Normal;
 
// Activate the form.
this.Activate();
}
 
private void menuItem1_Click(object Sender, EventArgs e)
{
this.check=false;
 
notifyIcon1.Visible = false;
 
Application.Exit();
 
this.Close();
}
 
private void InitializeComponent()
{
//
// Form1
//
//this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
//this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.ShowInTaskbar = false;
this.Load += new System.EventHandler(this.Form1_Load);
 
}
 
private void Form1_Load(object sender, System.EventArgs e)
{
 
}
public void HideApp()
{
this.WindowState = FormWindowState.Minimized;
Hide();
}
public void ShowApp()
{
Show();
this.WindowState = FormWindowState.Normal;
}
[STAThread]
 
static void Main()
{
Application.Run(new Form1());
 
}
 

 
}
 

 
Smile | :) GÜL Smile | :)
GeneralRe: Urgent Help Needed :context menu does not appear Pinmemberjchrisrenfrow13-Jul-05 8:22 
Questioncan it put an icon to upper taskbar of PPC(CE)? Pinmemberdosxp12-Jul-05 20:09 
AnswerRe: can it put an icon to upper taskbar of PPC(CE)? Pinmemberjchrisrenfrow13-Jul-05 7:33 
QuestionHow to use it Pinmembergbhanuprakash10-Jul-05 21:27 
AnswerRe: How to use it Pinmemberjchrisrenfrow11-Jul-05 5:07 
GeneralRe: How to use it Pinmembermarcosacramento19-Jul-06 6:50 
GeneralRe: How to use it PinmembersCHween26-Jul-06 5:18 
GeneralRe: How to use it Pinmembermatju843-Oct-07 22:04 

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 | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 1 Jul 2005
Article Copyright 2005 by jchrisrenfrow
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid