Problem with overriding closing event.

Berkil

Newcomer
Joined
Oct 14, 2009
Messages
4
Hello

My origional goal was to override the X button, so instead of closing it would minimize to the task bar. I reached my goal by using the following code:
Code:
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }

Now the only way the program could be closed was by pressing the 'Close' menu item.
Code:
        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
            Application.Exit();
        }

This just opens a new issue: I CAN'T SHUT DOWN MY COMPUTER!!!. While the program is running (on a windows XP machine) the machine wouldn't shut down. It would be much appriciated if someone could tell me another way to override the X button or just some other way to solve my problem :P
 
hmm, could you give me an example. I don't see how or where i should put this in my code :P.. the code name.CloseReason only works with FormClosingEventArgs. How do i mix it :P
 
Nevermind, i figured it out. Thank you very much. If you see any flaw in my result please say so :P

Code:
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                Hide();
            }
            else
            {
                Close();
                Application.Exit();
            }
        }

Isn't it weird that it's only machines running XP, who encounter this error ?.
 
Last edited:
Back
Top