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

By , 1 Jul 2005
 

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

About the Author

jchrisrenfrow
United States United States
Member
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 | :)

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralRestore and AxMediaPlayermemberJLCarbwood16 Aug '05 - 16:34 
Hi Chris,
 
I just found this class and its very nice, easy to apply to my application.
 
The one question I have is,
 
1 of my application use axmediaplayer.dll and when it's minimize to the
tray, and restore again the application restore fine, but the player size
change to the maximize size. I look thru the class and try somethings here
and there, but no luck.
 
I'm very new in programming, so maybe it's something very simple but I can't
picture it.
 
Thank you,
 
Jorge
 

GeneralRe: Restore and AxMediaPlayer Pinmemberjchrisrenfrow16 Aug '05 - 16:49 
Hi Jorge,
 
Although I am not familiar with the axmediaplayer dll, I would assume that the problem is likely with anchoring. Is the media player within your main window? If so, make sure you set the anchoring property correctly. The class changes the size of the main form (Me)...so if objects on that form are not anchored properly, there can be some problems when restored.
 
If you continue to have problems, let me know and I will try to help. If you like, you can send me your app and I can take a look at it.
 
Most people think that programming is an exact science...HA!Confused | :confused:
 
Later,
Chris

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 1 Jul 2005
Article Copyright 2005 by jchrisrenfrow
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid