[GIS] How to cancel SaveEdits/StopEdits button press (ArcObjects)

arcgis-10.0arcobjectsc

I have an interesting situation.Suppose a user creates 10 features in ArcMap in an edit session. Now the user tries to save this edits by clicking save edits or stop edits button. I would be performing some checks at this point(by listening to OnStopEditing/OnSaveEdits events). If the result of this check is not satisfactory, I would want ArcMap not to commit these changes. Also the state of edits not should be the same as it was before the Save Edits/Stop Edits button was pressed.

So in the case of failed checks, the 10 digitized features would stay ther in map, without permanantly committing to the database.

I hope I am not complicating things. Another way to describe the situation is, If the validations fail, ArcMap should just behave as if the user had not presed the StopEdits/SaveEdits button.

How can I do this using ArcObjects? I tried as shown below. But it simply saves edits and then stops editing. Anyone got ideas?

void editor_OnStopEditing(bool save)
{
  if (save == true)
  {
    if(HasMyValidationFailed())
    {
      //do not stop edit here..
      return; //did not work..
      //throw; //did not work..
    }
  }
}

Best Answer

Typically with event models at the completion of a task the event is called after the event is assumed to be completed. I know, doesn't make much sense, see code comments.

void editor_OnStopEditing(bool save)
{
  // I have already queued this operation for saving.  You need to stop me.
  // if (save == true)
  // Other:  Please never do this is makes programmers cry
  if (save)
  {
    if(HasMyValidationFailed())
    {
      return; //did work.  But I have already decided to save.
      throw; //did work.  But I threw an error for this event and not for this edit

    }
  }
}

You need to call UndoEditOperation most likely this. I am not sure your implementation since you didn't post all of it, but that method will help you out. Also your should check the HasEdits Method before you check for your Validation and other operations.

It has to be set up this way since assuming that you don't want to save creates a headache for the system. Listeners are optional (and very nice). If it is assumed that at the end of this operation that you don't want to save then everyone would have to create a listener to and explicitly say save or the system would have to check and see if another listener besides the already existing listeners has been added and then based on checking all the listeners decide that the system should save.