How to stop a delete

935
5
Jump to solution
12-12-2017 11:43 AM
MKa
by
Occasional Contributor III

I want to stop or warn a user of the deletion of a feature in the RowDeletedEvent.  When the user tries to delete the field I want to ask them if they are sure.  I want to roll back or something if they don't want to delete the item.

protected static void onRowDeleteEvent(RowChangedEventArgs args)
        {
            try
            {
                MessageBox.Show("Are you Sure", "Are you Sure", System.Windows.MessageBoxButton.YesNo, System.Windows.MessageBoxImage.Warning);
                //ROLL BACK HERE?

            }
            catch (Exception e)
            {
                ProMapBlackLogWriter.logError("ProMapBlackModule - onRowDeleteEvent", e);
            }
        }
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

Sorry i should have included a more complete example. You can check the guid of the row passed in to prevent reentrancy. This method is really the only place to catch/stop deletes from all sources.

    //Guid _lastEdit = Guid.Empty;
    private void onRowDeletedEvent(RowChangedEventArgs obj)
    {
      if (_lastEdit != obj.Guid)
      {
        //cancel with no notice
        //obj.CancelEdit(() => Task.FromResult(false));

        //cancel with dialog
        obj.CancelEdit("Delete Event\nAre you sure", true);
        _lastEdit = obj.Guid;
      }
    }

View solution in original post

5 Replies
by Anonymous User
Not applicable

You can use the RowChangedEventArgs.CancelEdit method. The options are limited at this time but it was designed for this use case.

obj.CancelEdit("Delete Event\nAre you sure", true);
MKa
by
Occasional Contributor III

Where can i call this from?  If I call it from the OnRowDeleteEvent, and then select yes, I get a loop right.  I need to catch when the user hits the delete button, and this is the only place right?

0 Kudos
by Anonymous User
Not applicable

Sorry i should have included a more complete example. You can check the guid of the row passed in to prevent reentrancy. This method is really the only place to catch/stop deletes from all sources.

    //Guid _lastEdit = Guid.Empty;
    private void onRowDeletedEvent(RowChangedEventArgs obj)
    {
      if (_lastEdit != obj.Guid)
      {
        //cancel with no notice
        //obj.CancelEdit(() => Task.FromResult(false));

        //cancel with dialog
        obj.CancelEdit("Delete Event\nAre you sure", true);
        _lastEdit = obj.Guid;
      }
    }
MKa
by
Occasional Contributor III

I wanted to add my finished code to help others who might want to do this.  The out of box message box didn't looks right, so I made this simple solution to stop a delete, with the help of Sean's code.

public static long CurrentlyDeletingOID = 0;
protected static void onRowDeleteEvent(RowChangedEventArgs args)
{
    try
    {
        long _deletingOID = args.Row.GetObjectID();
        if (CurrentlyDeletingOID != _deletingOID) 

            MessageBoxResult mbr = ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Are you sure you want to Delete this Feature?", "Confirm Delete",
                                                                                    MessageBoxButton.YesNo, MessageBoxImage.Warning);

            if (mbr.Equals(MessageBoxResult.Yes))
            {
                //PROCEED with the delete
                args.CancelEdit(() => Task.FromResult(true));

                //Need to set this so that the code doesn't get stuck in loop, no need to reset
                CurrentlyDeletingOID = _deletingOID;
            }
            else
            {
                //CANCEL the Delete
                args.CancelEdit(() => Task.FromResult(false));
            }

            //cancel with dialog using ESRI commands
            //args.CancelEdit("Delete Event\nAre you sure", true);

        }
    }
    catch (Exception e)
    {
        logError("onRowDeleteEvent", e);
    }
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
BrianBulla
Occasional Contributor III

Hi @Anonymous User,

I'm trying to implement your code above, but with one additional level.  On the features that I do NOT want to delete, I actually need to modify one of their attributes and then get out of the delete procedure.  Is there a simple way of doing this?  Potentially many features could be selected, so I need to accomodate for more than just one feature at a time.

I'm thinking I could keep track of GUIDs and LayerNames in an array of sorts, and then deal with them at the end of the OnRowDeleted procedure, but just looking for some pro advice.

I'm pretty sure this line is where I am losing my edit.....so I need to cancel the 'delete' but keep my edit....if that makes sense.

args.CancelEdit(() => Task.FromResult(false));

 

This is what I have so far:

protected void OnRowDeleted(RowChangedEventArgs args)
        {
            try
            {
                var row = args.Row;

                if (CurrentlyDeletingOID != args.Guid)
                {
                    //If this is a MAXIMO feature, just update the LifeCycleStatus field, but keep the feature
                    if (row.FindField("MXCREATIONSTATE") != -1)
                    {
                        //update to REMOVED and cancel the delete
                        row["LifeCycleStatus"] = "REMOVED";
                        args.CancelEdit(() => Task.FromResult(false));
                    }
                    else    //if any other feature, just delete it
                    {
                        //PROCEED with the delete
                        args.CancelEdit(() => Task.FromResult(true));
                    }

                    CurrentlyDeletingOID = args.Guid;
                }   
            }

            catch (Exception e)
            {
                ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(e.ToString());
            }
        }

 

 

0 Kudos