By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,387 Members | 1,729 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,387 IT Pros & Developers. It's quick & easy.

PLS HELP! - Detect closing a UserControl before disposed

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
MuZZy wrote:
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

Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.