Solved

Notify Icon Class and Popup Menu

Posted on 2007-12-05
8
653 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
8 Comments
 
LVL 48

Assisted Solution

by:jpaulino
jpaulino earned 50 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 85

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
 

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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 75 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now