PLS HELP! - Detect closing a UserControl before disposed

  • Thread starter Thread starter MuZZy
  • Start date Start date
M

MuZZy

HI,

I have a user control, say with one textBox.
I need the following: when the user closes form (either calling Form.Close, or pressing "X" or anyhow),
UserControl copies a file from a predefined folder to a folder in the textBox.

UserControl doesn't have a "Closing" event, so i tried to override UserControl.Dispose() :

// ==========================================================
protected override void Dispose( bool disposing )
{
// Here what i've added
File.Copy(SourceFile, TextFolder.Text + DestFile);
// If you check TextFolder.Text value, it's = "" for some reason...

// From here VS generated
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}

base.Dispose( disposing );
}
// ==========================================================

The problem is that when i get to Dispose function, TextFolder.Text value is somehow blank.
I don't really understand why, as the UserControl is not disposed yet, but all controls located on the usercotrol,
lose values when get into Dispose.

Any ideas would be very, very, very appreciated!

Thank you,
Andrey
 
You could try something like this:

Add an event handler for the load event in your control, and subscribe
to the parent's (or whoever it is) closing event. Pass a delegate to a
method to be called at closing time.

private void UserControl1_Load(object sender, System.EventArgs e)
{
((Form)this.Parent).Closing +=
new CancelEventHandler(ParentClosing);
}

private void ParentClosing(object sender, CancelEventArgs e)
{
// TODO: Add code...
}

Regards,
Joakim
 
Back
Top