12 Replies - 17070 Views - Last Post: 13 December 2009 - 04:28 PM Rate Topic: -----

#1 cabbae  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 11-December 09

How to minimize program to notification area(system tray)

Posted 11 December 2009 - 09:32 AM

Hello my names Abraham and I'm trying to create a program that when I press the X or _ buttons on the form it minimizes to the notification area(system tray). I did find some stuff but didn't understand were to put the code I just have some basic knowledge in VB.net. What I read was something about the formclosing event

	Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
		Me.WindowState = FormWindowState.Minimized

		e.Cancel = True

	   'Me.Visible = False
	End Sub



and well it sort of works to what I want but don't know were to go from there... what I read from another post was that I had to do something with "NotifyIcon Class" but still didn't know what to do with that or where to put the code.

Basically what I want the program to do is
minimize to system tray and a tooltip will pop up when I hover my mouse over it and display something that I want. Also when I right click the icon I would like some options to be displayed so that I could program it to close.

How could I Achieve this?

Is This A Good Question/Topic? 0
  • +

Replies To: How to minimize program to notification area(system tray)

#2 Shadar  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 157
  • Joined: 26-May 09

Re: How to minimize program to notification area(system tray)

Posted 11 December 2009 - 01:52 PM

View Postcabbae, on 11 Dec, 2009 - 08:32 AM, said:

How could I Achieve this?



Read about the NotifyIcon COMPONENT in the MSDN library.

This post has been edited by Shadar: 11 December 2009 - 01:53 PM

Was This Post Helpful? -1
  • +
  • -

#3 cabbae  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 11-December 09

Re: How to minimize program to notification area(system tray)

Posted 11 December 2009 - 06:03 PM

View PostShadar, on 11 Dec, 2009 - 12:52 PM, said:

View Postcabbae, on 11 Dec, 2009 - 08:32 AM, said:

How could I Achieve this?



Read about the NotifyIcon COMPONENT in the MSDN library.


I did and I didn't understand it... thats why I'm asking.
Was This Post Helpful? -1
  • +
  • -

#4 kasbaba  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 133
  • Joined: 03-November 08

Re: How to minimize program to notification area(system tray)

Posted 12 December 2009 - 10:35 AM

View Postcabbae, on 11 Dec, 2009 - 05:03 PM, said:

I did and I didn't understand it... thats why I'm asking.


Okay do this step by step
1. Add a NotifyIcon control.
2. Set the icon property of the NotifyIcon control to an icon of your choice.
3. Now add the following code:

	Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
		e.Cancel = True
		Me.WindowState = FormWindowState.Minimized
		Me.Hide()
	End Sub

	Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
		Me.WindowState = FormWindowState.Normal
		Me.Show()
	End Sub



So what I am doing here is Hiding the form and make it appear as being minimized to the SysTray.
Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#5 cabbae  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 11-December 09

Re: How to minimize program to notification area(system tray)

Posted 12 December 2009 - 08:35 PM

@kasbaba thanks for the reply and it works to some extent but there is a problem with it, when the double click event is triggered it dosen't really show the form it just resizes the form and places it in a corner were you can see it and I had to right click it in the explorer bar and move it so I could find it but other than that it worked like a charm ok now can you point me as to how I can add a tooltip to the icon and also a right click event for that notifyicon where it would show me a menustrip? Thank you so much it's coming alive now!!!!

This post has been edited by cabbae: 12 December 2009 - 08:36 PM

Was This Post Helpful? -1
  • +
  • -

#6 remorseless  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 129
  • Joined: 08-August 09

Re: How to minimize program to notification area(system tray)

Posted 12 December 2009 - 08:51 PM

View Postcabbae, on 13 Dec, 2009 - 01:35 PM, said:

@kasbaba thanks for the reply and it works to some extent but there is a problem with it, when the double click event is triggered it dosen't really show the form it just resizes the form and places it in a corner were you can see it and I had to right click it in the explorer bar and move it so I could find it but other than that it worked like a charm ok now can you point me as to how I can add a tooltip to the icon and also a right click event for that notifyicon where it would show me a menustrip? Thank you so much it's coming alive now!!!!


You can try to maximize it? So something like:
	Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
		Me.WindowState = FormWindowState.Maximised
		Me.Show()
	End Sub



Also, learn how to use NotifyIcons and ContextMenus. You can have a contextmenu appear when you right click a notification icon.
Was This Post Helpful? 0
  • +
  • -

#7 cabbae  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 11-December 09

Re: How to minimize program to notification area(system tray)

Posted 12 December 2009 - 09:25 PM

ok my icons name = nicoMinimizedState and I found an event thats called MouseMove and I did this:

Private Sub nicoMinimizedState_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles nicoMinimizedState.MouseMove
		nicoMinimizedState.ShowBalloonTip(100000, "Test", "Hello World", ToolTipIcon.None)
End Sub



the thing is that whenever the mouse moved the ballon tip always showed somtimes even when it didn't move, is there a way I can make it only appear when I specificaly hover my mouse over my icon?

I just did this for now:
Private Sub nicoMinimizedState_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles nicoMinimizedState.MouseDown
		nicoMinimizedState.ShowBalloonTip(100000, "Test", "Hello World", ToolTipIcon.None)
End Sub



also I found this other option for the menuStrip that I want to add to my icon which is nicoMinimizedState.ContextMenuStrip but have no idea how to use it for now still looking. Also I still haven't been able to fix the problem when the form reapears after minimizing it...

This post has been edited by cabbae: 12 December 2009 - 09:32 PM

Was This Post Helpful? 1
  • +
  • -

#8 cabbae  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 11-December 09

Re: How to minimize program to notification area(system tray)

Posted 13 December 2009 - 07:27 AM

@remorseless

Quote

You can try to maximize it? So something like:
	Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
		Me.WindowState = FormWindowState.Maximised
		Me.Show()
	End Sub



Also, learn how to use NotifyIcons and ContextMenus. You can have a contextmenu appear when you right click a notification icon.



Thanks for the idea, but I tried that already didn't help. Though I was looking into a property of the form called FormBorderSyle to see if I could set it to a fixed sized and not let the form be resized...
Was This Post Helpful? -1
  • +
  • -

#9 kasbaba  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 133
  • Joined: 03-November 08

Re: How to minimize program to notification area(system tray)

Posted 13 December 2009 - 08:02 AM

View Postcabbae, on 12 Dec, 2009 - 07:35 PM, said:

@kasbaba thanks for the reply and it works to some extent but there is a problem with it, when the double click event is triggered it dosen't really show the form it just resizes the form and places it in a corner were you can see it and I had to right click it in the explorer bar and move it so I could find it but other than that it worked like a charm ok now can you point me as to how I can add a tooltip to the icon and also a right click event for that notifyicon where it would show me a menustrip? Thank you so much it's coming alive now!!!!


To show the form Centered after a DoubleClick on NotifyIcon:
	Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
		Me.WindowState = FormWindowState.Normal

		Dim intX As Integer = Screen.PrimaryScreen.Bounds.Width
		Dim intY As Integer = Screen.PrimaryScreen.Bounds.Height

		Me.Left = (intX - Me.Width) / 2
		Me.Top = (intY - Me.Height) / 2

		Me.Show()
	End Sub



To add a ToolTip to NotifyIcon:
		NotifyIcon1.Text = "Your ToolTip Text"



To add RightClick (ContextMenu) to NotifyIcon:
First Add a ContextMenuStrip from ToolBox (Menus & Toolbar)
Add Menus/Sub Menus as Required
Add Code to Menus/Sub Menus as Required
Go to the NotifyIcon's ContextMenuStrip property and select the ContextMenuStrip that you just added.

You are done !!!
Was This Post Helpful? 0
  • +
  • -

#10 cabbae  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 11-December 09

Re: How to minimize program to notification area(system tray)

Posted 13 December 2009 - 09:55 AM

Hey thanks for everything but I found something better than messing with the y & x coordinates and I changed the program a bit and very satisfied with the outcome! Though I want to thank everyone who contributed and helped in the process. Here is my code:

'Author	: Abraham Cabrera
'Date	  : December 13, 2009
'Purpose   : This is a test program that minimizes to the system tray and 
'			when you click the notify icon it shows a context menu.
Imports System.IO

Public Class frmMain
	'Sets the Notify icon visiblity to false so that when the program starts you can't see it.
	Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		nfiSmiley.Visible = False
	End Sub
	'Allows for the user to see the label.
	Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click

		lblHelloWorld.Enabled = True
	End Sub
	'Disables the label so that you can't see it.
	Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
		lblHelloWorld.Enabled = False
	End Sub
	'When you double click the notify icon in the system tray(notification area) the form automatically shows
	'and the notify icon disapears.
	Private Sub nfiSmiley_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles nfiSmiley.MouseDoubleClick
		Me.Show()
		Me.StartPosition = FormStartPosition.CenterScreen
		Me.WindowState = FormWindowState.Normal

		nfiSmiley.Visible = False
	End Sub
	'This event handles the part where you minimize the main form, hides it and shows the notify icon.
	Private Sub frmMain_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
		If Me.WindowState = FormWindowState.Minimized Then
			Me.Hide()
			nfiSmiley.Visible = True
		End If
	End Sub
	'Shows the about this program form, this handles to events. One from the main menu and the other 
	'from the context menu.
	Private Sub cmnHelp_About_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles cmnHelp_About.Click, mnuHelp_About.Click
		frmAbout.StartPosition = FormStartPosition.CenterScreen
		frmAbout.ShowDialog()
	End Sub
	'Close the application
	Private Sub cmnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles cmnExit.Click, mnuFile_Exit.Click
		Me.Close()
	End Sub
	'Confirms the users exit.
	Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
		If MessageBox.Show("Are you sure you want to exit?", "Smiley", MessageBoxButtons.YesNo, MessageBoxIcon.Question) _
		   = Windows.Forms.DialogResult.No Then
			e.Cancel = True
		End If
	End Sub

	'Reads the instructions from a fiel and adds it to the list box of the form instructions
	'then it displays the form.
	Private Sub mnHelp_Insrtuctions_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles mnHelp_Insrtuctions.Click, cmnHelp_Instructions.Click
		Dim inRead As System.IO.StreamReader
		Dim inFile As System.IO.File

		inRead = inFile.OpenText("instructions.txt")

		Do Until inRead.Peek = -1
			frmInstructions.lstInstructions.Items.Add(inRead.ReadLine)
		Loop

		inRead.Close()

		frmInstructions.Show()
	End Sub

	Private Sub cmnPreferences_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmnPreferences.Click
		'Right now it does nothing....
	End Sub
End Class



Ok here are some pictures of my test app running:

Posted Image

Basically what I did was make the program confirm its exit. When it is also minimized I hide the form and show the notify icon. I added a menuStrip and the contextMenuStrip to link to the NotifyIcon. This part that I didn't understand but later I found out that you had to go to the NotifyIcon properties and select the contextMenuStip created and select and select it. The contexMenu is programmed the same way as a normal menuStrip so it wasn't that hard to understand. So that's it.

I'm going to upload my project folder later so you guys can see it and others can benefit from it.

Edit

Heres the link: project

This post has been edited by cabbae: 13 December 2009 - 10:51 AM

Was This Post Helpful? 1
  • +
  • -

#11 cabbae  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 11-December 09

Re: How to minimize program to notification area(system tray)

Posted 13 December 2009 - 03:31 PM

could I get a sticky on this thread so that when others ask about it they can just come here...
Was This Post Helpful? 0
  • +
  • -

#12 vorpeder999  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-November 09

Re: How to minimize program to notification area(system tray)

Posted 13 December 2009 - 03:40 PM

Add a Notifycation icon and then when it shoud hide to tray, just write:

Me.Hide()
Me.ShowInTaskbar = False



then when doubleclick on Notifycation icon:

Me.Show()
Me.ShowInTaskbar = True


Was This Post Helpful? 0
  • +
  • -

#13 cabbae  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 11-December 09

Re: How to minimize program to notification area(system tray)

Posted 13 December 2009 - 04:28 PM

@!vorpeder999

I already finished it... but thanks. I found out how to add all the stuff needed and how to show the tooltip for the NotifyIcon which would be called a Ballontip.

nfiSmiley.ShowBalloonTip(15000,"test", "hello World",ToolTipIcon.None)



I just want to add a sticky to this thread for others.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1