Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Notify Icon Class and Popup Menu

Posted on 2007-12-05
8
Medium Priority
?
681 Views
Last Modified: 2008-02-01
Hello,
I found the code below here on EE website and it looks very simple when comparing to the other code(s) regarding the same subject
(e.g. http://msdn2.microsoft.com/en-us/library/system.windows.forms.notifyicon.aspx).

1./ Is the code below the right solution for minimizing the application into system tray ?
2./ If yes, would it be possible to add a code for Popup Menu when right clicked the icon in the System Tray?
3./ I have tried the http://msdn2.microsoft.com/en-us/library/system.windows.forms.notifyicon.aspx
code as well and I am getting some errors. Could you also tell me why?

Error1
'Protected Overrides Sub Dispose(disposing As Boolean)' has multiple definitions with identical signatures. C:\Documents and Settings\PC\My Documents\Visual Studio 2005\Projects\SystemTrayMSWeb\SystemTrayMSWeb\Form1.Designer.vb      7      29      SystemTrayMSWeb
Error      2      'notifyIcon1' is already declared as 'Friend Dim WithEvents notifyIcon1 As System.Windows.Forms.NotifyIcon' in this class.      C:\Documents and Settings\PC\My Documents\Visual Studio 2005\Projects\SystemTrayMSWeb\SystemTrayMSWeb\Form1.vb      10      23      SystemTrayMSWeb
Error      3      'components' is already declared as 'Private Dim components As System.ComponentModel.IContainer' in this class.      C:\Documents and Settings\PC\My Documents\Visual Studio 2005\Projects\SystemTrayMSWeb\SystemTrayMSWeb\Form1.vb      11      13      SystemTrayMSWeb
Warning      4      'Public Sub New()' in designer-generated type 'WindowsApplication1.Form1' should call InitializeComponent method.      C:\Documents and Settings\PC\My Documents\Visual Studio 2005\Projects\SystemTrayMSWeb\SystemTrayMSWeb\Form1.vb      18      16      SystemTrayMSWeb




***************** Code which worked for me. I need to add a Popup Menu ****************
Add a NotifyIcon to your Form.  Now set the Icon() property to the Icon you want to appear in the System Tray.  Next set the Visible() property to False so it does not appear in the Tray initially.

To make the form "minimize to the tray", handle the Resize() event:

Public Class Form1

    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        If Me.WindowState = FormWindowState.Minimized Then
            NotifyIcon1.Visible = True
            Me.Hide()
        End If
    End Sub

    Private Sub NotifyIcon1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotifyIcon1.DoubleClick
        Me.Show()
        Me.WindowState = FormWindowState.Normal
        NotifyIcon1.Visible = False
    End Sub

End Class

Thanks a lot guys!
0
Comment
Question by:PSG_Calgary
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 48

Assisted Solution

by:jpaulino
jpaulino earned 150 total points
ID: 20409733
If I understand it right you need only to add a menu when you click on the system tray icon.

Add a contextmenustrip to your form and define your menus. Then add this line of code:

' In the form load event
NotifyIcon1.ContextMenuStrip = Me.myContextMenuStrip

0
 
LVL 9

Expert Comment

by:sognoct
ID: 20409755
I've tried the example and it works fine, but of course it can't find the icon required in the source code :

    ' The Icon property sets the icon that will appear
    ' in the systray for this application.
    notifyIcon1.Icon = New Icon("appicon.ico")

you should change this line into :

    notifyIcon1.Icon = New Icon("c:\project...\myicon.ico")

The error maybe is due to another form1 defined into the same project. Try changing the line :

Public NotInheritable Class Form1

into

Public NotInheritable Class myIconForm ...
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 20412187
"Error      2      'notifyIcon1' is already declared as 'Friend Dim WithEvents notifyIcon1 As System.Windows.Forms.NotifyIcon' in this class."

The example on MSDN is creating the components at RUN-TIME...while the instructions you found on EE direct you to add the components to the form at DESIGN-TIME.  Combining the two approaches results in TWO "NotifyIcon" controls being declared and thus the error.

Adding to jpaulino's comments...you can also set the ContextMenu at design-time.  Add and define the menus for the ContextMenuStrip.  Then select the NotifyIcon control on your form.   Next in the Properties Pane, change the ContextMenuStrip DropDown (remember the NotifyIcon control was last selected!) to the name of your dropdown.

I added an "Exit" option to my ContextMenuStrip for demonstration purposes:

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:PSG_Calgary
ID: 20417505
Hello,

'jpaulino' it worked for me, thanks.
Regarding the example (msdn). I created new project with myForm1, pasted the code in it and changed the path to the icon (in the code). That's all I did. And I still have some errors. I don't know what is wrong. Could you please look at that again?

Error      1      'Protected Overrides Sub Dispose(disposing As Boolean)' has multiple definitions with identical signatures.      C:\Documents and Settings\PC\My Documents\Visual Studio 2005\Projects\SystemTrayMSWeb\myForm1.Designer.vb      7      29      SystemTrayMSWeb
Error      2      'components' is already declared as 'Private Dim components As System.ComponentModel.IContainer' in this class.      C:\Documents and Settings\PC\My Documents\Visual Studio 2005\Projects\SystemTrayMSWeb\myForm1.vb      10      13      SystemTrayMSWeb
Warning      3      'Public Sub New()' in designer-generated type 'WindowsApplication1.myForm1' should call InitializeComponent method.      C:\Documents and Settings\PC\My Documents\Visual Studio 2005\Projects\SystemTrayMSWeb\myForm1.vb      15      16      SystemTrayMSWeb


Thanks again
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public NotInheritable Class myForm1
    Inherits System.Windows.Forms.Form
 
    Private contextMenu1 As System.Windows.Forms.ContextMenu
    Friend WithEvents menuItem1 As System.Windows.Forms.MenuItem
    Friend WithEvents notifyIcon1 As System.Windows.Forms.NotifyIcon
    Private components As System.ComponentModel.IContainer
    <System.STAThread()> _
    Public Shared Sub Main()
        System.Windows.Forms.Application.Run(New myForm1)
    End Sub 'Main
    Public Sub New()
        Me.components = New System.ComponentModel.Container
        Me.contextMenu1 = New System.Windows.Forms.ContextMenu
        Me.menuItem1 = New System.Windows.Forms.MenuItem
 
        ' Initialize contextMenu1
        Me.contextMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.menuItem1})
 
        ' Initialize menuItem1
        Me.menuItem1.Index = 0
        Me.menuItem1.Text = "E&xit"
 
        ' Set up how the form should be displayed.
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Text = "Notify Icon Example"
 
        ' Create the NotifyIcon.
        Me.notifyIcon1 = New System.Windows.Forms.NotifyIcon(Me.components)
 
        ' The Icon property sets the icon that will appear
        ' in the systray for this application.
        notifyIcon1.Icon = New Icon("C:\Documents and Settings\PC\My Documents\Visual Studio 2005\Projects\SystemTrayMSWeb\App.ico")
 
        ' The ContextMenu property sets the menu that will
        ' appear when the systray icon is right clicked.
        notifyIcon1.ContextMenu = Me.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 (NotifyIcon example)"
        notifyIcon1.Visible = True
    End Sub 'New
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        ' Clean up any components being used.
        If disposing Then
            If (components IsNot Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub 'Dispose
    Private Sub notifyIcon1_DoubleClick(ByVal Sender As Object, ByVal e As EventArgs) Handles notifyIcon1.DoubleClick
        ' Show the form when the user double clicks on the notify icon.
 
        ' Set the WindowState to normal if the form is minimized.
        If (Me.WindowState = FormWindowState.Minimized) Then Me.WindowState = FormWindowState.Normal
 
        ' Activate the form.
        Me.Activate()
    End Sub
    Private Sub menuItem1_Click(ByVal Sender As Object, ByVal e As EventArgs) Handles menuItem1.Click
        ' Close the form, which closes the application.
        Me.Close()
    End Sub
    End Class 'Form1

Open in new window

0
 
LVL 9

Expert Comment

by:sognoct
ID: 20418804
- create a new project
- add new item
- select class
- paste the code
- delete form1
- delete :    
   <System.STAThread()> _
    Public Shared Sub Main()
        System.Windows.Forms.Application.Run(New myForm1)
    End Sub 'Main
- put the class as startup in the project properties
0
 

Author Comment

by:PSG_Calgary
ID: 20420978
soqnoct,

I have no errors now!
However, it doesn't do exactly what I want.
- when I run it, there is an application Icon in System Tray already (it shouldn't be there at this point. it should be there when I minimize it only)

- when I minimize it stay in the the taskbar. it should disappear and stay in system tray only.

thanks
0
 
LVL 9

Accepted Solution

by:
sognoct earned 225 total points
ID: 20426505
change
  NotifyIcon1.Visible = TRUE

into
 NotifyIcon1.Visible = False

THEN add the


Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
MyBase.OnResize(e)
if  Me.WindowState=  FormWindowState.Minimized then
 NotifyIcon1.Visible = True
 me.visible = false
end if
End Sub

(haven't tried it, so maybe there can be an error on this sub ... )
0
 

Author Comment

by:PSG_Calgary
ID: 20432857
soqnoct,

I added those two changes into the code and it works better,however on doubleclick, when it is in system tray, it doesn't do anything. It should restore as it was when app started.

Could you try to change the code (attached) so it works and send the code back?

I appreciate your help.



Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public NotInheritable Class Form1
    Inherits System.Windows.Forms.Form
 
    Private contextMenu1 As System.Windows.Forms.ContextMenu
    Friend WithEvents menuItem1 As System.Windows.Forms.MenuItem
    Friend WithEvents notifyIcon1 As System.Windows.Forms.NotifyIcon
    Private components As System.ComponentModel.IContainer
    Public Sub New()
 
        Me.components = New System.ComponentModel.Container
        Me.contextMenu1 = New System.Windows.Forms.ContextMenu
        Me.menuItem1 = New System.Windows.Forms.MenuItem
 
        ' Initialize contextMenu1
        Me.contextMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() _
                            {Me.menuItem1})
 
        ' Initialize menuItem1
        Me.menuItem1.Index = 0
        Me.menuItem1.Text = "E&xit"
 
        ' Set up how the form should be displayed.
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Text = "Notify Icon Example"
 
        ' Create the NotifyIcon.
        Me.notifyIcon1 = New System.Windows.Forms.NotifyIcon(Me.components)
 
        ' The Icon property sets the icon that will appear
        ' in the systray for this application.
        notifyIcon1.Icon = New Icon("C:\Documents and Settings\Drahos\My Documents\Visual Studio 2005\Projects\SystemTrayMSWeb\SystemTrayMSWeb.ico")
 
        ' The ContextMenu property sets the menu that will
        ' appear when the systray icon is right clicked.
        notifyIcon1.ContextMenu = Me.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 (NotifyIcon example)"
        notifyIcon1.Visible = True
    End Sub 'New
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        ' Clean up any components being used.
        If disposing Then
            If (components IsNot Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub 'Dispose
    Private Sub notifyIcon1_DoubleClick(ByVal Sender As Object, ByVal e As EventArgs) Handles notifyIcon1.DoubleClick
        ' Show the form when the user double clicks on the notify icon.
 
        ' Set the WindowState to normal if the form is minimized.
        If (Me.WindowState = FormWindowState.Minimized) Then _
            Me.WindowState = FormWindowState.Normal
 
        ' Activate the form.
        Me.Activate()
    End Sub
    Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
        MyBase.OnResize(e)
        If Me.WindowState = FormWindowState.Minimized Then
            NotifyIcon1.Visible = True
            Me.visible = False
        End If
    End Sub
    Private Sub menuItem1_Click(ByVal Sender As Object, ByVal e As EventArgs) Handles menuItem1.Click
        ' Close the form, which closes the application.
        Me.Close()
    End Sub
    Private Sub InitializeComponent()
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
        Me.SuspendLayout()
        '
        'Form1
        '
        Me.ClientSize = New System.Drawing.Size(221, 181)
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.Name = "Form1"
        Me.ResumeLayout(False)
 
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        notifyIcon1.Visible = False
    End Sub
End Class 'Form1

Open in new window

0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question