Save changes before Close()

  • Thread starter Tilfried Weissenberger
  • Start date
T

Tilfried Weissenberger

Hi all,

Following Scenario:

I have a Windows.Forms.Form - a user clicks the inherited close-window
icon on the title-bar of that form to close it.

1) That already calls Form.Close() - doesn't it?
2) Where should I call Form.Dispose()?
3) Now if I want to add a "Save changes first?" question into the
Form_Closing() event, I noticed that some of the controls (TextBox for
example) no longer serve the entered value in the .Text attribute! So
I cannot save the changes from within this event!?!

thanks!

regards, Tilli
 
B

Ben S. Stahlhood II

Tilfried,

You should have a flag that is set when the form is "dirty", meaning has not
been saved yet. In the Form "Closing" event you can then check the flag,
prompt the user and if they want to save set the e.Cancel = true and then
call a SaveSettings() method that you will need to make. If this is your
main window there is no need to call Form.Dispose.

Hope that helps...

Take care,
Ben S. Stahlhood II
 
T

Tilli

Hi,

thanks for the tip! But I cannot call SaveSettings() from within the
closing event, right? So how would I go about calling that outside? Do I
need to use a timer in each form?

My problem was that some of the controls were not available anymore, but
are they instantainiously available again when I set e.Cancel to true?

regards, Tilli
 
B

Ben S. Stahlhood II

Tilli,

You should create a new method "private void SaveSettings()" that you call
from the ClosingEvent. You want to set the e.Cancel = true and then call
the method. You should be able to access all your controls from the new
method... let me know if this helps or not.

Take care,
Ben S. Stahlhood II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top