.NET Windows Forms - Intercepting Close X Event

This should be a dumb question, but I can't figure it out. I also can’t use the constructor, because the coders before me managed to drop the GUI and the logic all in one, so now it is confusing. I have to do it in the old school.

I have a form that can be closed in three ways: the Close button, the File / Close menu, and the X icon. I want them all to do the same. Capturing a button and menu events is easy. In fact, both are connected to the 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 X , I tried: this.FormClosing +=new FormClosingEventHandler(this.onCloseConfig); I believe that this is what causes an infinite loop. I don't want this :) FormClosed is another option, but it seems too late. I just want to intercept the fact that X was pressed, and not the fact that the form is closing.

+7
winforms
source share
5 answers

I think that you want to close the form, what you might be missing is to check the reason for closing and only cancel it if it belongs to the user, for example.

 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" Form.ControlBox = false , but it will also save you min / max.

+12
source share

Create a separate button / menu closing method:

 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 completely disable the close button ("X") by setting this.ControlBox = false

+3
source share

Btw, is there a better name for this method?

Close [FormName] might be a better choice.

Or it should be this.Close ();

Yes, if all you want is to close the form.

You need to create a new event handler for the FormClosing event. The parameter is FormClosingEventArgs, not EventArgs. In the button and menu item click handlers, just do it. Close ().

This will trigger an event, and you can place all your checks there.

+1
source share

The FormClosed event occurs after the form is closed. This is definitely not what you want.

The FormClosing event is more likely what you need.

The FormClosing event is fired when any button or menu link is clicked. Everything that tries to close the form will raise the FormClosing event.

Now, a more appropriate way to use FormClosingEventArgs would be the following in your FormClosing event handler method:

 if(m_configControl.Modified) e.Cancel = true; 

therefore, if configuration management is not changed, the form will continue to close. You want it to not close only if there are changes or unsaved changes.

EDIT 2 . After re-reading your question, see below my changes in the upper code:

 if(m_configControl.Modified) if(DialogResult.OK == MessageBox.Show("Do you want to save changes?", "Changes detected") SaveChanges(); 

Then, the user is prompted for unsaved changes and saved only if the user clicked OK in the MessageBox. Please note that you will have to add the appropriate buttons to the additional parameters of the MessageBox.Show () method.

If you want to perform "Yes / No / Cancel when formatting", you will have to go as follows:

 if(m_configControl.Modified) { DialogResult dr = MessageBox.Show(...); switch(dr) { case DialogResult.OK: SaveChanges(); break; case DialogResult.No: // Do nothing... break; case DialogResult.Cancel: e.Cancel = true; } } 

So, when the user clicks β€œYes”, your application will save the changes. When he clicks "No", the application will do nothing and continue to close. When the user wants to return to the application, Close will be closed.

EDIT 1 Take a look at my answer to This question , which seems completely what you want to accomplish.

+1
source share

This should stop closing the window: FormClosing (object sender, System.EventArts e) {if (e.CloseReason == CloseReason.UserClosing) e.Cancel = true; }

0
source share

All Articles