override close event (C#)

solus

Freshman
Joined
Apr 5, 2003
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:
M

mutant

Guest
I dont get how to do that too...
Why would they make such a simple thing work like that?
 

Bucky

Contributor
Joined
Dec 23, 2001
Location
East Coast
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.
 

solus

Freshman
Joined
Apr 5, 2003
Location
Phoenix
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.
 

Bucky

Contributor
Joined
Dec 23, 2001
Location
East Coast
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.
 
Top Bottom