-1

Is there any way to run the application on the background even if the application/form is closed. All i can do now is just minimize it.

    private void Form2_Resize(object sender, EventArgs e)
    {

        if (WindowState == FormWindowState.Normal)
        {
            Hide();
            WindowState = FormWindowState.Minimized;
        }
        else if (FormWindowState.Normal == this.WindowState)
        {
            Show();

        }
    }
discable10
  • 37
  • 7

1 Answers1

0

If your application (your process) is closed, the notify icon will disappear. There is no way to avoid that.

So what you probably want is to keep your application running even if the user closes your Form. One way to achieve this is to actually not close the form but just hide it.

Therefor you need to subscribe to the Closing event of the Form:

public class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.Closing += Form1_Closing;
    }
}

and implement the event handler like this:

void Form1_Closing(object sender, FormClosingEventArgs e)
{
    Hide();
    e.Cancel = true;            
}

By setting e.Cancel = true you tell the form not to close. So you are just hiding it.

You'll need to add some code to your notify icon to reopen (Show()) the form again.

René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • where do i put this code : this.Closing += Form1_Closing; – discable10 May 23 '16 at 11:47
  • @discable10 you can add this to the constructor of your Form (like I showed in my updated answer) or you can set this event in the designer. – René Vogt May 23 '16 at 11:49
  • somehow this part doesn´t work "Form1_Closing". It is highlighted. – discable10 May 23 '16 at 11:59
  • @discable10 what means "highlighted"? what is the compiler error, if there is one?. From the code you showed I assume you are able to write an event handler. My answer is an example that you'll need to adapt to your specifif `Form`. – René Vogt May 23 '16 at 12:03
  • Form2_Closing is the error and the error is ; The name Form2_Closing does not exist in the current context. – discable10 May 23 '16 at 12:20
  • @discable10 rename my `Form1_Closing` to `Form2_Closing` – René Vogt May 23 '16 at 12:21