Harold Finch
Harold Finch

Reputation: 586

NotifyIcon doesn't hide if form appears

I made a modality that if the form2 of my application is minimized, the NotifyIcon appears in the system tray, this working very good. The NotifyIcon disappear when the user execute a double click on it, later the form2 appear again. The problem is if the form is minimized and the user open the form1 no with double click on NotifyIcon but by the Navigation Menu, the form appears correctly but the NotifyIcon don't disappears. I've see that if I minimized the form again I also have two NotifyIcon in the system tray. What happens this?

double click code (working correctly) - form1

Private Sub NotifyIcon1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
    Me.Show()
    Me.WindowState = FormWindowState.Normal
    NotifyIcon1.Visible = False
End Sub

menu navigation code on form1

Private Sub RisultatiToolStripMenuItem3_Click(sender As Object, e As EventArgs) Handles RisultatiToolStripMenuItem3.Click
        Dim res As New Risultati
        res.de_active()
    End Sub

form2 de_active function (is Risultati form in the example)

Public Sub de_active() 
    If Application.OpenForms().OfType(Of Risultati).Any Then
        Me.Show()
        Me.WindowState = FormWindowState.Normal
        NotifyIcon1.Visible = False
    Else
        Me.Show()
    End If
End Sub

Upvotes: 0

Views: 784

Answers (2)

Harold Finch
Harold Finch

Reputation: 586

I've fixed adding an handler on my resizing form:

Private Sub Form1_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
Select Case Me.WindowState
    Case FormWindowState.Minimized
        'Show NotifyIcon
    Case FormWindowState.Normal
        'Hide NotifyIcon
    Case formWindowState.Maximized
        'hide ..
End Select
End Sub

Upvotes: 0

Bradley Uffner
Bradley Uffner

Reputation: 16991

It sounds like you are ending up with multiple instances of your form, each with it's own NotifyIcon. The code for your toolbar button is actually creating a new instance of the form. If you don't want multiple instances of your form you need to keep one variable around with that reference and hide or show that reference instead of creating a new one every time you want to display it.

Upvotes: 2

Related Questions