2

Silly thing I need help with.

When I run my application if I hit the X button to close the window, it stops the application. What I want is for it to hide the winform but keep running on the task bar (already have it running on the task bar).

Is there a simple solution for me to do this?

Sean
  • 897
  • 4
  • 20
  • 42
  • How long do you want to hide your form that way? How do you show it again (I guess by clicking on the icon on taskbar?) – King King Sep 07 '13 at 15:44
  • Yes I want the form to hide when the X is pressed then to open it again they need to go to the task bar and click on open (which I already have there). But for some reason, when I hit the X it closes both my winform and takes it away from the task bar. – Sean Sep 07 '13 at 15:45

3 Answers3

2

Keeping the taskbar button requires the window to stay alive. You can do so by just having the window minimize instead of close. Which requires implementing the FormClosing event, something like this:

    bool shouldTerminate;

    protected override void OnFormClosing(FormClosingEventArgs e) {
        if (e.CloseReason == CloseReason.UserClosing && !shouldTerminate) {
            e.Cancel = true;
            this.WindowState = FormWindowState.Minimized;
        }
        base.OnFormClosing(e);
    }
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

You can handle formclosing event and make it hide the form instead of closing it.

void myForm_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = true;
    this.Hide();
}
VahidNaderi
  • 2,448
  • 1
  • 23
  • 35
0

Rather than keep it running in Taskbar, what you need to do is to hide it on application load, and keep it running in System Tray area. The NotifyIcon control will help you achieve this behavior. You can find how to implement such behavior in this MSDN article.

Also see:

Creating Tray Applications in .NET: A Practical Guide

minimize app to system tray

How can I make a .NET Windows Forms application that only runs in the System Tray?

Community
  • 1
  • 1
S2S2
  • 8,322
  • 5
  • 37
  • 65