NotifyIcon goes crazy. VB.NET

2

I comment on my application quickly and then the problem:

I have two Forms, each of them has a button that opens the other and closes that one (changes form, go). The Form1 has a NotifyIcon for, in case of closing both windows, to reopen it. All this works well. The problem comes when I return from Form2 to Form1 , which creates another equal icon in the notification bar, there being two. If I go back to Form2 from Form1 nothing happens, but when I return to Form1 I create another icon and so infinitely, only going from Form2 to Form1 .

I leave the code that I think affects this to see if you can help me out because I'm not able to see the problem.

Form2 button that returns to Form1 :

Private Sub FTTA_Click(sender As Object, e As EventArgs) Handles FTTA.Click

    Dim goAlmacen As New FormAlmacen
    goAlmacen.Show()

    Me.Hide()
End Sub

Load and Notify of Form1

Private Sub FormAlmacen_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    vIn.Select()

    Dim r As Rectangle = My.Computer.Screen.WorkingArea
    Location = New Point(r.Width - Width, r.Height - Height)

    vIn.Text = String.Empty
    vOUT.Text = String.Empty
    txt1.Text = String.Empty
    txt2.Text = String.Empty
    vNombre.Text = String.Empty

End Sub


Private Sub Vnotificación_MouseClick(sender As Object, e As MouseEventArgs) Handles Vnotificación.MouseClick
    vIn.Select()
    Me.Show()
    vIn.Text = String.Empty
    vOUT.Text = String.Empty
    txt1.Text = String.Empty
    txt2.Text = String.Empty
    vNombre.Text = String.Empty
End Sub

Finally, in the design it is put in the following way, I do not know if it will have to see but just in case (I delete the parts of code that do not interest.)

Private Sub InitializeComponent()
    Me.Vnotificación = New System.Windows.Forms.NotifyIcon(Me.components)
    resources.ApplyResources(Me.Vnotificación, "Vnotificación")
End Sub
    Friend WithEvents Vnotificación As NotifyIcon

Maybe there is some way to "kill" an icon and replace it with another if it exists or something like that, but I'm not able to do it. Any ideas?

Edit:

Code of the button that leads from Form1 to Form2 :

  Private Sub ButtonVolver_Click(sender As Object, e As EventArgs) Handles FATT.Click

        Dim goTrabajos As New FormFases
        goTrabajos.Show()
        Me.Hide()

    End Sub
    
asked by CharlieMR 21.03.2018 в 08:59
source

2 answers

1

You have a serious problem with your code. You are hiding the active form when you go from one to another, and at that moment you create a new instance of the form to be displayed. Doing so, the NotifyIcon is the least of the problems, since you are uselessly generating forms that are being hidden and that there may come a time when you stay even without memory.

Since you are interested in the "main" form being hidden so that you can show it at will and that the NotifyIcon is always active, what you must do is add a constructor to the second form that receives the instance of the "main" form who is calling That way, having that instance available you can call your Show method instead of having to create a new one.

Summing up. Basically, in the form FormFases you create a variable of type FormAlmacen and a new constructor that receives the instance of the main form:

Dim formPrincipal As FormAlmacen

Public Sub New(Byval formp As FormAlmacen)
    formPrincipal = formp
End Sub

Now we have a reference to the main form. That way, when we want to show it we do the following:

Private Sub FTTA_Click(sender As Object, e As EventArgs) Handles FTTA.Click
    formPrincipal.Show()
    Me.Close()
End Sub

The only thing that we need to hide the FormAlmacen when we call FormFases , as you already had, and send the instance of FormAlmacen when creating the FormFases , sending the constructor Me as parameter:

Private Sub ButtonVolver_Click(sender As Object, e As EventArgs) Handles FATT.Click
    Dim goTrabajos As New FormFases(Me)
    goTrabajos.Show()
    Me.Hide()
End Sub

There are probably other better ways to manage it, but this is a simple solution.

    
answered by 21.03.2018 в 11:15
1

If your application is going to use more than one form and you want to handle notifyicon, the ideal is that you base your application on an ApplicationContext and do not use a form as an application.

You create a class that inherits from ApplicationContext:

Public Class AppContext
     Inherits ApplicationContext

     private withevents TrayIcon as NotifyIcon
     private withevents TrayIconContextMenu  as ContextMenuStrip
     private withevents CloseMenuItem as ToolStripMenuItem

     public Sub New()
          InitializeComponent()
          ' cualquier otra cosa que quieras inicializar
     End

     private Sub InitializeComponent()
          TrayIcon = new NotifyIcon()
          CloseMenuItem = new ToolStripMenuItem()
          TrayIconContextMenu = new ContextMenuStrip()

          ' aqui inicializas los tres componentes 

         TrayIcon.ContextMenuStrip = TrayIconContextMenu
     End

    private sub TrayIcon_DoubleClick(sender as object, e as EventArgs) Handles TrayIcon.DoubleClick
         ' aqui puedes crear los forms y mostrarlos
    End sub

     private sub CloseMenuItem_Click(sender as object, e as EventArgs) Handles CloseMenuItem.Click
          Application.Exit()
     End sub
End

And you change

Application.Run(new Formulario())

By

 Application.Run(new AppContext())
    
answered by 01.04.2018 в 08:28