Tray system icon

Ok, at first I just started C #, so I'm not quite the most experienced programmer. Okay, so here is my problem, which may seem silly to you guys;)

I have a fairly simple application that a friend asked me to. So far I have managed with a little Google, but I am stuck with this. The application works fine and comes down to the system tray and most efficiently from the system tray. However, when I open the second form from this application, it creates another icon in the system tray and starts duplicating every time I open another form. Therefore, as a result, I have many icons, and they are all separate instances of the main form. System Tray Events

private void notifyIcon_systemTray_MouseDoubleClick(object sender, MouseEventArgs e) { if (FormWindowState.Minimized == WindowState) { Show(); WindowState = FormWindowState.Normal; } } private void CronNecessityForm_Resize(object sender, EventArgs e) { notifyIcon_systemTray.Visible = true; if (FormWindowState.Minimized == WindowState) Hide(); } private void restoreContextMenuItem_Click(object sender, EventArgs e) { Show(); WindowState = FormWindowState.Normal; } 

To open a form:

 private void preferencesToolStripMenuItem_Click(object sender, EventArgs e) { CronPreferences.formPreferences CronPreferences = new CronPreferences.formPreferences(); CronPreferences.Show(); } 

Close it:

 private void button2_Click(object sender, EventArgs e) { this.Hide(); } 

How can I display all forms on the same icon in the system tray?

0
source share
2 answers

You will need one global tray icon, to which all of them will be available. Do this using a static variable that remains unchanged across all different instances of the class.

Then if you want:

  • Open one form: save the link to the last form in a variable and open it.
  • Open all minimized forms: go through each form and open them again.
+2
source

If I understand correctly, you want to save only one instance of your application . In this case, your title is a little misleading, as your problem has nothing to do with tray icons or multiple forms.

On the other hand, if you really have the main form in the application that opens the second form (which creates the tray icon), in this case you just need to make sure that your second form is created only once:

 public class MainForm { private SecondForm _secondForm; public void OpenSecondForm() { // create it only once if (_secondForm == null) _secondForm = new SecondForm(); // otherwise just show it _secondForm.Show(); } } 
0
source

Source: https://habr.com/ru/post/957369/


All Articles