Astrify Astrify - 1 year ago 70 Question

Multiple forms are duplicating my tray icons. Only occurs in certain forms

I am writing an application in VB.Net, and I have an issue in the fact that when I open my login form, my tray icon is duplicated. However, when I open my about form, it's not. Here's my code:

Private Sub lblAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblAbout.Click
End Sub
Private Sub btnLogIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogIn.Click
End Sub

That is literally all I am doing, yet it doesn't duplicate for About.vb, but does for Login.vb?

I've triple checked:

  • There are no tray icons on the login form.

  • The properties for BOTH forms are the same.

  • I'm definitely not calling them differently.

Its like a phantom tray icon -.-

How should I go about fixing this?

Note: if i'm being too vague, just let me know what info you want and I'll put it up :)

EDIT: I've added

ntfTray.Visible = False

To btnLogIn_Click(..... and it seems to fix it on closing Login.vb, but when the form is open there are still two.

Answer Source

My psychic debugging powers tell me you are using the default instances of your About and Login forms.

I highly recommend NOT using the default instances, forget that they exist, banish them from your mind forever.

You need to declare new instances of those forms, and hide / show those instances as needed, like this:

Dim frm as new About()

This should make sure that all your instances, and their persistent objects, such as the NotifyIcon don't get duplicated.

Depending on your application design you may need to create longer lived instances of these forms and keep around with longer lifetimes than simple method scope would allow. You can do that with class level fields.