Cannot 'Undo' after edits made

278
4
Jump to solution
11-24-2017 07:03 AM
Highlighted
Regular Contributor

I have some code where I edit fields using a Chained EditOperation, but they do no show up in the Undo stack.  Here is the main logic behind what I am doing:

var editOperation = new ArcGIS.Desktop.Editing.EditOperation();
editOperation.Name = "Create new Facility IDs";

 

< start the looping>

var featureEdit = new ArcGIS.Desktop.Editing.EditOperation();
featureEdit = editOperation.CreateChainedOperation();

 

<code to make edits>

 

featureEdit.Modify(insp);
featureEdit.Execute();

<end the loop>  //continue until all selected features are edited

 

editOperation.Modify(insp);  //Without this line, nothing shows in the Undo stack....but even with it, nothing                                                            actually gets Undone after I press Undo

editOperation.Execute();

I want the user to be able to Undo ALL of the edits, not just one at a time.

Any ideas as to where I am going wrong??

Thanks,

Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
Regular Contributor

Hi Sean,

Thanks for posting that.  I have finally figured out what is going on.

Apparently you need to do a Modify/Edit on your initial EditOperation in order for the ChainedOperation to register with the Undo Stack.  Or at least, that is the only way I can get it to work.

It seems a bit counter-intuitive though.  In my case, I am doing ALL of my editing in a loop.  In your sample, you do an edit before your loop begins.  I would think that in most 'real-world' examples if you were creating a loop to do some editing you wouldn't think you need to do that first edit in order to 'set-up' the rest of the edits.

Anyways, that's just my 2 cents.  Perhaps the documentation needs to be updated to better explain how this functionality works.

Doing this, seems to get around that initial edit issue:

//initial editop
var op = new EditOperation();
op.Name = "ChainedLoop";

//this basically edits nothing, but sets things up so you can use a chainedEdit

var insp = new Inspector();
insp.Load(lyr, 1);
op.Modify(insp);
op.Execute();

Thanks for your help! 

View solution in original post

Reply
0 Kudos
4 Replies
Highlighted
Esri Contributor

Brian, 

A chained operation must be executed after the principal edit operation.

In general the pattern is 

1. create edit operation

2.  do action(s)  (Create, Modify, Cut etc)

3. execute edit operation 

4. create chain operation from the edit operation

5. do action(s) on chain operation  (Create, Modify, Cut etc)

6. execute chain operation

This pattern will add one item to the undo stack (with the name assigned to the edit operation). All actions will be reversed on undo. 

Narelle

Reply
0 Kudos
Highlighted
Regular Contributor

Hi Narelle,

I am still having problems getting the edits to show up on the Undo stack.  Can you provide some sample code, or direct me to one of the samples that may demonstrate how to use this??

In my code I am doing edits on selected features through looping.  I want all of the edits to show up on one 'Undo' action.

Thanks,

Reply
0 Kudos
Highlighted
Esri Regular Contributor

Brian,

Here's some code where I'm reading and incrementing a field value from the first feature in a layer within one operation.

    protected override void OnClick()
    {
      //Run on MCT
      QueuedTask.Run(() =>
      {
        //find the layer
        var lyr = MapView.Active.Map.FindLayers("Ranger stations").First() as FeatureLayer;

        //initial editop
        var op = new EditOperation();
        op.Name = "ChainedLoop";
        IncrementTestField(lyr,op);
        op.Execute();

        //chain loop
        for (int i = 0; i < 10; i++)
        {
          var cop = op.CreateChainedOperation();
          IncrementTestField(lyr, cop);
          cop.Execute();
        }
      });
    }

    private void IncrementTestField(FeatureLayer lyr, EditOperation op)
    {
      //load the first feature from the layer
      //read the value and increment
      var insp = new Inspector();
      insp.Load(lyr, 1);
      var testValue = Convert.ToInt64(insp["TEST"]);
      testValue++;
      insp["TEST"] = testValue;
      op.Modify(insp);
    }
  }
Highlighted
Regular Contributor

Hi Sean,

Thanks for posting that.  I have finally figured out what is going on.

Apparently you need to do a Modify/Edit on your initial EditOperation in order for the ChainedOperation to register with the Undo Stack.  Or at least, that is the only way I can get it to work.

It seems a bit counter-intuitive though.  In my case, I am doing ALL of my editing in a loop.  In your sample, you do an edit before your loop begins.  I would think that in most 'real-world' examples if you were creating a loop to do some editing you wouldn't think you need to do that first edit in order to 'set-up' the rest of the edits.

Anyways, that's just my 2 cents.  Perhaps the documentation needs to be updated to better explain how this functionality works.

Doing this, seems to get around that initial edit issue:

//initial editop
var op = new EditOperation();
op.Name = "ChainedLoop";

//this basically edits nothing, but sets things up so you can use a chainedEdit

var insp = new Inspector();
insp.Load(lyr, 1);
op.Modify(insp);
op.Execute();

Thanks for your help! 

View solution in original post

Reply
0 Kudos