tags:

views:

47

answers:

3

hey guys, a simple question but i dont know to do it.

When I exit my app I want to call my save method to check if the user wants to save any changes.

How to i call the save click event method? This is what I came up with:

        if (isDirty == true)
        {
            saveToolStripMenuItem();
        }
        this.Close();

But that doesnt work

+1  A: 

If this is a winform application , there is form closing event , you can hook this event.

See here

saurabh
how do I hook into the closing event? I can find the "Load" event and the "Activated" event but nothing else.
Codie Vincent
+2  A: 

Create a Save method. Put the actual Save logic in the save-method.

  • Call the Save method from the saveToolStripMenuItem - click eventhandler.
  • Call the Save method when you close the form as well.
Frederik Gheysels
+1  A: 

I'd suggest you to use the Form class' FormClosing event; you can check whether there are unsaved changes and prompt the user whether he wants to save the changes (YesNoCancel dialog, if he presses Cancel, you can just set e.Cancel = true).

As for your direct example, I'd recommend you to put your actual saving logics into a Save() method, and call that; if that's not what you want, or is not possible, you can still manually invoke the Click event handler of the button or menuitem that actually performs the save, just make sure you specify the parameters that are actually used.

ShdNx