Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Windows Forms - intercepting the Close X event

Tags:

.net

winforms

This must be a dumb question, but I cannot figure it out. I also cannot use the designer because coders before me managed to throw GUI and logic all in one, so now it is confused. I've got to do it the old school way.

I have a Form which can be closed in 3 ways: Close button, File / Close menu, and the X icon. I want them all to do the same thing. Intercepting the button and the menu events is easy. In fact, both are hooked up to an onCloseConfig method. Btw, is there a better name for this method?

private void onCloseConfig(object sender, System.EventArgs e)
{
    if (! m_configControl.Modified)
    {
        Application.Exit(); // Or should it be this.Close();
    }
    ....
    // Else present a dialog, ask if they want to save.
}

So, to intercept the X I tried: this.FormClosing +=new FormClosingEventHandler(this.onCloseConfig); I believe this is what causes an infinite loop. I do not want that :) FormClosed is another option, but it seems too late. I just want to intercept the fact that the X was clicked, not the fact that the form is closing.

like image 649
Hamish Grubijan Avatar asked May 10 '10 15:05

Hamish Grubijan


People also ask

How can I tell if clicked X or close button?

To detect if the user clicked either X or your CloseButton, you may get it through the sender object. Try to cast sender as a Button control, and verify perhaps for its name "CloseButton", for instance. private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (string. Equals((sender as Button).

Which event that has happen when you close your form?

The Closing event occurs as the form is being closed. When a form is closed, all resources created within the object are released and the form is disposed. If you cancel this event, the form remains opened.

How do I close a form in Winforms?

To close a form, you just need to set the form's DialogResult property (to any value by DialogResult. None ) in some event handler. When your code exits from the event handler the WinForm engine will hide the form and the code that follows the initial ShowDialog method call will continue execution.

What is close () in C#?

Close() function is used to close a Form in a Windows Form application in C#. We can use the Form. Close() function inside the button click event to close the specified form by clicking a button.


2 Answers

I think you do want form closing, the thing you may be missing is to check the reason for closing and only cancel it if it is from the user e.g.

private void my_FormClosing(object sender, FormClosingEventArgs e)
{
  if (e.CloseReason == CloseReason.UserClosing)
  {
    e.Cancel = true; //I'm sorry Dave, I'm afraid I can't do that.
  }
}

to hide the 'X' set Form.ControlBox = false, but this will get rid of min/max as well.

like image 139
jk. Avatar answered Sep 20 '22 15:09

jk.


Create a separate method for the button/menu close:

private void myButton_Click(object sender, EventArgs e)
{
    this.Close();
}

private void myMenuButton_Click(object sender, EventArgs e)
{
    this.Close();
}

private void myForm_FormClosing(object sender, FormClosingEventArgs e)
{
    if (m_configControl.Modified)
    {
        var result = MessageBox.Show("Name Of Application", "Would you like to save before closing?", MessageBoxButtons.YesNoCancel);
        if(result == DialogResult.Yes)
            //Save here
        else if(result == DialogResult.Cancel)
            e.Cancel = true;
    }
}

Or, you can disable the close-button ("X") altogether by setting this.ControlBox = false

like image 39
BlueRaja - Danny Pflughoeft Avatar answered Sep 20 '22 15:09

BlueRaja - Danny Pflughoeft