Click here to Skip to main content
11,802,076 members (58,571 online)
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 77.7K 689 63
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.


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].


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


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.


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


About the Author

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 | :)

You may also be interested in...

Comments and Discussions

GeneralThis is pretty good! Pin
Ant210021-Feb-10 2:03
memberAnt210021-Feb-10 2:03 
GeneralExcellent code!! Pin
Lee Green23-Oct-07 9:18
memberLee Green23-Oct-07 9:18 
GeneralWorks Flawlessly! Pin
Leerz27-Oct-06 3:29
memberLeerz27-Oct-06 3:29 
GeneralTrayText Property Pin
Fluid Dynamics Software23-Feb-06 5:09
memberFluid Dynamics Software23-Feb-06 5:09 
QuestionCan't get it to work... Pin
steveengland9-Sep-05 4:21
membersteveengland9-Sep-05 4:21 
GeneralRestore and AxMediaPlayer Pin
JLCarbwood16-Aug-05 16:34
memberJLCarbwood16-Aug-05 16:34 
GeneralRe: Restore and AxMediaPlayer Pin
jchrisrenfrow16-Aug-05 16:49
memberjchrisrenfrow16-Aug-05 16:49 
GeneralUrgent Help Needed :context menu does not appear Pin
Gul Hunerkar13-Jul-05 3:17
memberGul Hunerkar13-Jul-05 3:17 
GeneralRe: Urgent Help Needed :context menu does not appear Pin
jchrisrenfrow13-Jul-05 7:22
memberjchrisrenfrow13-Jul-05 7:22 
Questioncan it put an icon to upper taskbar of PPC(CE)? Pin
dosxp12-Jul-05 19:09
memberdosxp12-Jul-05 19:09 
AnswerRe: can it put an icon to upper taskbar of PPC(CE)? Pin
jchrisrenfrow13-Jul-05 6:33
memberjchrisrenfrow13-Jul-05 6:33 
QuestionHow to use it Pin
gbhanuprakash10-Jul-05 20:27
membergbhanuprakash10-Jul-05 20:27 
AnswerRe: How to use it Pin
jchrisrenfrow11-Jul-05 4:07
memberjchrisrenfrow11-Jul-05 4:07 
GeneralRe: How to use it Pin
marcosacramento19-Jul-06 5:50
membermarcosacramento19-Jul-06 5:50 
GeneralRe: How to use it Pin
sCHween26-Jul-06 4:18
membersCHween26-Jul-06 4:18 
GeneralRe: How to use it Pin
matju843-Oct-07 21:04
membermatju843-Oct-07 21: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
Web04 | 2.8.151002.1 | Last Updated 1 Jul 2005
Article Copyright 2005 by jchrisrenfrow
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid