Select to view content in your preferred language

How to stop onDeleteFeature

862
8
06-30-2022 11:03 AM
BrianBulla
Regular Contributor II

Hi,

I'm working with the OnDeleteFeature Event.  Is there a way to stop the 'Delete' from happening?  I'm trying to code the event so if a certain criteria is met, then it won't delete the feature.  But if not met, then the feature can be deleted.

Here is my code....pretty simple.  The way I have it now, everything gets deleted.  How do I stop the delete process??

 

 

 

        private void Events_OnDeleteFeature(IObject obj)
        {
            try
            {
                //Check to see if current obj is a Feature.
                if (obj is IFeature)
                {
                    //Cast to an IFeature
                    IFeature inFeature = (IFeature)obj;
                    ITable inTable = obj.Table;

                    //Look for the Maximo field and edit.
                    if (inFeature.Fields.FindField("MXCREATIONSTATE") != -1)
                    {
                        inFeature.set_Value(inFeature.Fields.FindField("LifeCycleStatus"), "REMOVED");
                        //MessageBox.Show("SET TO REMOVED");
                        return;
                    }
                    else
                    {
                        MessageBox.Show("DELETE IT");
                    }
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error in onDeleteFeature procedure.");
            }
        }

 

 

   

0 Kudos
8 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

Long time ago when IClassExtensions have started to die we have found solution on Russia Esri distributors forum. There is no such page now. I have checked  our old code and found that we call function like this:

 

        /// <summary>
        /// Abort editing operation.
        /// </summary>
        public void AbortOperationWithoutStopEditing()
        {
            try
            {
                m_ipEditor.AbortOperation();
            }
            catch (Exception ex)
            {
                throw;
            }
        }

 

where m_ipEditor is IEditor type class variable.

Solution author wrote about strange messages, but solution works pretty good for us.

0 Kudos
DuncanHornby
MVP Notable Contributor

Just thinking out aloud here, your logic may be flawed as according to the help file it says

Whenever a new feature is deleted the OnDeleteFeature event is fired.

So this event is fired after the deletion and I assume because its deleted there is nothing to check.

What do you do when you normally delete something on a map, you select it first then press the delete button, may be it would be more logical to check for a change in selection event then you verify it is suitable for deletion?

GKmieliauskas
Esri Regular Contributor

Hi,

You can do checking if you have your own tools for deleting. But you are not sure that customer will not press Delete button on Attribute table window or somewhere else.

0 Kudos
BrianBulla
Regular Contributor II

Thanks for posting that code.  I will give it a try later today.  Yes, I thought about creating a button, but as you said, if the operator decides to delete using another method then that does not work.

0 Kudos
BrianBulla
Regular Contributor II

Yes, there seems to be no event that fires before the delete actually happens, so I'm hoping to be able to exit out of the delete somehow.  I will try the code above and see if that works.  If not, then I'll start looking into other things.

Thanks!

0 Kudos
BrianBulla
Regular Contributor II

OK, so I kind of have something working but it only works on one Feature at a time.  If I have multiple selected where I need it to just update an attribute (instead of deleting it), then I get the following message and my code stops.

BrianBulla_0-1656952825725.png

I'm guessing this is some sort of system message that displays once theEditor.StopOperation("STOP"); gets called.  Is there a way to bypass this message??

If so, then the rest of my code would run which should re-start the delete process and catch the remaining selected items.  Sort of an intentional recursive loop is what I am going for.

 

private void Events_OnDeleteFeature(IObject obj)
        {
            IMap map = pMxDoc.FocusMap;

            try
            {
                //Check to see if current obj is a Feature.
                if (obj is IFeature)
                {
                    //Cast to an IFeature
                    IFeature inFeature = (IFeature)obj;
                    ITable inTable = obj.Table;

                    //Look for the Maximo field and edit.
                    if (inFeature.Fields.FindField("MXCREATIONSTATE") != -1)
                    {
                        inFeature.set_Value(inFeature.Fields.FindField("LifeCycleStatus"), "REMOVED");
                        //MessageBox.Show("SET TO REMOVED");
                        theEditor.StopOperation("STOP");

                        //if there are still selected features then not all have finished deleting....go again
                        if (map.SelectionCount > 0)
                            SendKeys.SendWait("DELETE");                                               
                    }
                    else
                    {
                        //MessageBox.Show("DELETE IT");
                    }
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error in onDeleteFeature procedure.");
            }
        }

 

 

0 Kudos
BrianBulla
Regular Contributor II

How I'm testing this is to select a bunch of features.  Some that need to be deleted and some that just need to be set to "REMOVED".  So far, all of the ones that need to be deleted actually get deleted, but with the "REMOVED" ones only the first one gets processed (ie. set to "REMOVED"), and then the code stops once the error message is displayed.

0 Kudos
GKmieliauskas
Esri Regular Contributor

Hi, 

I think my suggested workflow is suitable only for simple delete operation. It stops all features deleting on first wrong case.

0 Kudos