Minimize to system tray

Visual basic 2008 Express

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 159235 Views - Last Post: 27 December 2010 - 12:16 AM Rate Topic: -----

#1 VbProgrammer08  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 31-May 08

Minimize to system tray

Posted 31 May 2008 - 07:20 AM

Hi! I've looked all over the internet and did a search on Dreamincode.net but I can't find anything on minimizing to the tray for Vb 2008.

What I want to achieve is if the user hits the "X" button at the top, the application should minimize to the tray. When I right-click on the icon, a context menu strip should appear.

Right now I don't have any code because I have no idea how to achieve this.

Please help.. I will greatly appreciate it :)

Is This A Good Question/Topic? 0
  • +

Replies To: Minimize to system tray

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Minimize to system tray

Posted 31 May 2008 - 07:36 AM

First off, VB 2008 is VB.Net so this belongs in that forum (dont worry Ill move it for you). What you are wanting to accomplish is easier than you would think. Read up on the Form.FormClosing Event, this is the event that is fired as the application is closing.

Here is a short example of using this event


'This is the event that is fired as the application is closing, whether it
'be from a close button in the application or from the user
'clicking the X in the upper right hand corner
Private Sub Form1_FormClosing(sender as Object, e as FormClosingEventArgs)   Handles Form1.FormClosing
         'What we will do here is trap the closing of the application and send the application
         'to the system tray (or so it will appear, we will just make it invisible, re-showing
         'it will be up to you and your notify icon)

        'First minimize the form
         Me.WindowState = FormWindowState.Minimized

        'Now make it invisible (make it look like it went into the system tray)
        Me.Visible = False
End Sub



Now as far as controlling your application from an icon in the system tray, read into the NotifyIcon and how to assign a ContextMenu to the icon control.

Hope this gets you headed down the right path :)
Was This Post Helpful? 1
  • +
  • -

#3 VbProgrammer08  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 31-May 08

Re: Minimize to system tray

Posted 31 May 2008 - 01:21 PM

Thanks PsychoCoder, it's exactly what I wanted to achieve. 2 :^: up :)
Was This Post Helpful? 0
  • +
  • -

#4 VbProgrammer08  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 31-May 08

Re: Minimize to system tray

Posted 31 May 2008 - 05:47 PM

EDIT: I found an error. When I hit the exit button, it does minimize and go to the system tray, but it also exits the program after hitting OK on the MsgBox.

This is the FormClosing function you gave me. The "nfi" is the NotifyIcon. And the second subroutine is a Context menu item that belongs to the "nfi" NotifyIcon.

So basically I have nfi, a NotifyIcon control. And a context menu strip called "ContextSysTray" with a menu item called "Show Window".
	Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
		'First minimize the form
		Me.WindowState = FormWindowState.Minimized

		'Now make it invisible (make it look like it went into the system tray)
		Me.Visible = False

		nfi.Visible = True

		MsgBox("FTProgram has been minimized to the task bar.")

	End Sub

	Private Sub ShowWindowToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ShowWindowToolStripMenuItem.Click
		nfi.Visible = False
		Me.WindowState = FormWindowState.Maximized
		Me.Visible = True
	End Sub



How do I make it so the X button does not exit the program but runs the FormClosing sub. By doing this I will get the system tray thing to work :D Or is there a better way?

Thanks. Your help is very appreciated. I owe you one.
Was This Post Helpful? 1

#13 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Minimize to system tray

Posted 31 May 2008 - 06:09 PM

My bad, I actually forgot a single line of code that will stop the application from closing. I forgot to add e.Cancel = True, telling it to not close the application


Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
        'First minimize the form
        Me.WindowState = FormWindowState.Minimized

        'Now make it invisible (make it look like it went into the system tray)
        Me.Visible = False

        'Cancel the closing of the application
        e.Cancel = True

        nfi.Visible = True

        MsgBox("FTProgram has been minimized to the task bar.")

End Sub


Was This Post Helpful? 1

#14 VbProgrammer08  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 31-May 08

Re: Minimize to system tray

Posted 01 June 2008 - 03:03 PM

Thank you, it worked perfectly.
Was This Post Helpful? 0
  • +
  • -

#15 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Minimize to system tray

Posted 01 June 2008 - 05:25 PM

Wow, I've learned so much from these forums already, and it STILL continues to amaze me how little I actually know! I didn't know you could make the form do stuff on close, although I had a feeling(I just didn't know the command(s)).

In short, you guys are awesome!
:)

My coding was a little different than your closing event sub...just the heading.

Private Sub frmNewChar_FormClosing(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Disposed


And to my surprise, it worked fine. I only recently looked at yours closely to find that it was different. I went by process of elimination, mostly since I didn't know what was supposed to come after Handles MyBase..

Again, in short, these forums are AWESOME!

This post has been edited by Locke37: 01 June 2008 - 05:30 PM

Was This Post Helpful? 0
  • +
  • -

#16 GameDeveloper84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-August 08

Re: Minimize to system tray

Posted 14 August 2008 - 11:32 AM

Okay this is helpfull for me up to a point. How would one do this useing VB.NET 2005.
Was This Post Helpful? 0
  • +
  • -

#17 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Minimize to system tray

Posted 14 August 2008 - 11:36 AM

This code is in VB.NET 2005 :)
Was This Post Helpful? 0
  • +
  • -

#18 GameDeveloper84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-August 08

Re: Minimize to system tray

Posted 14 August 2008 - 11:41 AM

Okay, my bad, i typed something in worng.

one other thing what is the declared value of nfi?

This post has been edited by GameDeveloper84: 14 August 2008 - 11:44 AM

Was This Post Helpful? 0
  • +
  • -

#19 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Minimize to system tray

Posted 14 August 2008 - 12:29 PM

nfi is a NotifyIcon Control
Was This Post Helpful? 1

#20 ArcticFox  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 05-April 09

Re: Minimize to system tray

Posted 05 April 2009 - 09:19 AM

View PostPsychoCoder, on 14 Aug, 2008 - 11:29 AM, said:

nfi is a NotifyIcon Control


Name 'nfi' is not declared


What can I do to fix this

(Sorry I am new to this)

This post has been edited by ArcticFox: 05 April 2009 - 09:37 AM

Was This Post Helpful? 0
  • +
  • -

#21 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Minimize to system tray

Posted 05 April 2009 - 09:54 AM

Drag a NotifyIcon control onto your form and name it nfi
Was This Post Helpful? 0
  • +
  • -

#22 joker100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-July 09

Re: Minimize to system tray

Posted 13 July 2009 - 06:10 PM

View PostPsychoCoder, on 5 Apr, 2009 - 08:54 AM, said:

Drag a NotifyIcon control onto your form and name it nfi



i don't understand this


e.Cancel = True


I'm new to :)
Was This Post Helpful? 0
  • +
  • -

#26 LoveIsNull  Icon User is offline

  • Recovering D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 646
  • Joined: 10-March 09

Re: Minimize to system tray

Posted 13 July 2009 - 06:41 PM

[attachment=12761:attachment]

Don't understand how to drag a NotifyIcon to the form or e.Cancel = True?
This instance of e is a FormClosingEventArgs object, Cancel is a boolean (true or false) property that decides if the application should cancel the close (true means don't close).

Note that e is not always FormClosingEventArgs, the specific type depends but generally speaking it is some kind of event argument class.
Was This Post Helpful? 1

  • (2 Pages)
  • +
  • 1
  • 2