0

In short, I have this:

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
            e.Cancel = true;
            this.WindowState = FormWindowState.Minimized;
            this.ShowInTaskbar = false;
    }

And it's not working (program closes when pushed "X", instead of my destination - minimizing it); what could be the problem?

this.ShowInTaskbar = false; // is used because app goes to system tray and it doesn't need to be showed in taskbar.
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Tautvydas
  • 1,268
  • 7
  • 19
  • 33

2 Answers2

2

Why bother testing to see what state the window is currently in?

Just set e.Cancel to true and set the state to minimized.

=== update ===

I created a new Windows Forms project (tested both 4.0 and 3.5 frameworks, using VS2010 and VS2012).

From there I added the notify icon to Form1 and set the doubleclick event (code below) and the icon to some random icon file I had. I also set the forms formclosing event (code below):

public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void Form1_FormClosing( object sender, FormClosingEventArgs e ) {
            e.Cancel = true;
            this.WindowState = FormWindowState.Minimized;
            this.ShowInTaskbar = false;
        }

        private void notifyIcon1_MouseDoubleClick( object sender, MouseEventArgs e ) {
            this.WindowState = FormWindowState.Normal;
        }
    }

Then I ran the project. When I clicked the close button, the form was hidden. When I double clicked the notify icon it came back. I'm on Windows 7, but that shouldn't matter.

Now, I did find that if I made Form1 a child form, then some weirdness occurred. But I'm not entirely sure that's what you are doing.

NotMe
  • 87,343
  • 27
  • 171
  • 245
  • True, removed the if (updated code), still not work. On pushing close button app actualy closes.. – Tautvydas Jan 07 '13 at 22:13
  • @Tautvydas: I just put together a tiny sample project. The only thing it had was a single form and a formclosing event. The event only contained `e.Cancel = true;` and `this.WindowState = FormWindowState.Minimuzed;` When I tried to close, it minimized as expected. Are you sure the `ShowInTaskbar=false;` part isn't making you think it closed? You might want to drop that particular code line. – NotMe Jan 07 '13 at 22:19
  • I just tried it in same way and it closed as it is closing in main app. Could it be connected with .net framework version? (mine is 3.5) – Tautvydas Jan 07 '13 at 22:25
  • Still same, I use Visual Studio 2008. Doesn't work at the moment I changed at form properies value of ControlBox to false, which removes windown control buttons, still there is problem with alt+f4. Could it be problem that I have changed some settings in form properties? – Tautvydas Jan 07 '13 at 22:56
  • @Tautvydas: Just tried alt+f4, behaves as expected. I don't have 2008 to test with. Although I don't see why that would matter. Sorry out of ideas. – NotMe Jan 07 '13 at 23:21
0

IIRC, you can't have this.ShowInTaskbar set to false and have it minimized; in order to do what you're trying to do you'd need this:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = true;
    this.Visible = false;
}

Then in whatever you're using to bring the form back to standard view, you would just put:

this.Visible = true;

If you're still looking for the minimize animation, just minimize the form before you set the visibility.

jssblck
  • 529
  • 5
  • 20
  • Technically you can. The form just completely hides itself though. – NotMe Jan 07 '13 at 22:21
  • Ah, ok. I thought I had remembered it causing odd problems for me at one point- I guess I was remembering incorrectly :) – jssblck Jan 07 '13 at 22:22