Code Beast
Code Beast

Reputation: 475

C# WinForms application showing many tray icons

I'm super new to C# and this is my third question here regarding it.

I'm making an app which can be minimized to the system tray. I have two forms named Form1 and Form2. What I have done so far is:

In Form1, I have a button which is showing the Form2 using this code:

this.Hide();
Form2 form2 = new Form2();
form2.Show();

The Form2 has a button which is hiding it, using this code:

this.Hide();

Now, I have the tray icon on the task bar. The tray icon has a ContexMenuStrip, and there is an option named show using this code:

Form1 form1 = new Form1();
form1.Show();

The problem is that when I click on it, a second tray icon is appearing on the taskbar. Both tray icons have the same menu and both are working. If I click on show again another window with Form1 pops up, and there are three tray icons, and so on....

Can someone help me?

Upvotes: 1

Views: 2089

Answers (4)

Code Beast
Code Beast

Reputation: 475

Here is a working code in case that someone is looking for it:

Form1:

/* Hiding Form1 and showing Form2 */    
private void btnHideForm1_Click(object sender, EventArgs e)
             {
                     Form mod = new Form2();
                     mod.Owner = this;
                     mod.Show();
                     this.Hide();
         }

Form2:

    /* Hiding Form2 and showing Form1 */
private void btnHideForm2_Click(object sender, EventArgs e)
         {
                 this.Owner.Show();
                 this.Close();
         }

Thanks for your help guys!!!

I LOVE YOU!!!

Upvotes: 0

Matt Becker
Matt Becker

Reputation: 2368

You can prevent windows from having icons in the taskbar by settings ShowInTaskbar to false on the form. However, the other answers are correct when they say you're creating a new form over and over.

Why would you want two forms to show at the same time? Should they both be on the screen at the same time and be active at the same time? If so, you might try a MDI interface. http://en.wikipedia.org/wiki/Multiple_document_interface

It's possible in WinForms, but I think Microsoft is moving away from them in WPF.

Upvotes: 1

dmi_
dmi_

Reputation: 1257

The issue is that you are making a new instance of Form1. This creates a brand new window instead of reviving your old one.

Form1 form1 = new Form1();
    form1.Show();

You need to have Form2 reference the original instance of Form1. You can make a constructor to pass in a self reference that would look like

Form2 form2 = new Form2(this);

Upvotes: 4

aquinas
aquinas

Reputation: 23796

It's because you are creating a new Form1 everytime.

Form1 form1 = new Form1();

You don't want to create a new Form1, you want to show the old one. Give Form2 a reference to your first form1 (call it theMainform1 for example). And then instead of

Form1 form1 = new Form1();
form1.Show();

You want to do

theMainform1.Show();

So you would have:

 this.Hide();
 Form2 form2 = new Form2();
 form2.theMainform1 = this;
 form2.Show();

Upvotes: 5

Related Questions