override close event (C#)

solus

Freshman
Joined
Apr 5, 2003
Messages
30
Location
Phoenix
override close event

How can I override the close event on a child window so that i can cancel it being closed without effecting the parent window?

Here's the code i'm using to cancel the close event in my child window:

C#:
		private void StatusForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			e.Cancel = true;
		}

Here is the code I use to open the child window initially:

C#:
			StatusForm chStatusForm = new StatusForm();
			chStatusForm.MdiParent = this;
			chStatusForm.Show();
 
Last edited:
I dont get how to do that too...
Why would they make such a simple thing work like that?
 
solus, you are currently not overriding the base class method you
mentioned, you just made a subroutine. You need to either:

a.) Change the sub name to OnClosing and add the override
keyword, and change the parameters, like this:
C#:
private override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
        }

or...
b.) Make sure that the current sub is a delegate of the Closing event.
 
I want it so the child window won't close unless the parent window is closed. Both bits of code effects both the child and parent so neither can be closed.
 
In that case, create a boolean variable that can be accessed from
both forms. When the parent form closes, set the boolean value to
true. In the child form, check the value of the boolean variable,
and if it's true then unload the form. You'll also probably want to
set the variable back to false after closing the form.
 
Back
Top