2

I am using ArcObjects in a C# addin with ArcGIS 10.3.1 for Desktop.

I'm trying to stop a user from removing certain layers from the Table of contents. I have my listeners setup for IActiveViewEvents_ItemDeletedEventHandler but want to know how/what to capture the layer that is removed? Can this be done, or can I only inform the user that they removed something?

4
  • 3
    IIRC, the item passed to the handler can be cast to an ILayer (assuming it was a layer that was deleted). Since deleting a layer is an undoable operation, you might try calling IOperationStack.Undo, assuming it's already been added to the stack when ItemDeleted fires. Jan 4, 2016 at 20:39
  • 1
    if (typeof(Object) is ILayer) { pLayer = (ILayer)Object;.... there are a few items that that event keeps track of and you must make sure that it's a layer that's being removed before you try to cast and use it. From there you can determine if it's raster/feature/group/web layer and act appropriately. Kirk's suggestion of calling the undo should work but if it doesn't you could simply re-add the layer upon removal. Jan 4, 2016 at 21:38
  • @KirkKuykendall I tired to see what is on the operation stack, and the item is not added there. (the edit session may or may not be on) The 'remove' action is happening from the right-click menu, so I'm not sure where those events are sent, if at all.
    – Omnia9
    Jan 5, 2016 at 14:52
  • As Kirk Kuykendall feared, the ItemDeleted operation is not added to the stack until after the ItemDeleted event fires. So if you put OperationStack.Undo() in the ItemDeletedHandler sub it will not undo the operation you want it to undo because that operation hasn't been added to the stack yet. Jan 6, 2016 at 16:22

1 Answer 1

1

Thanks to @KirkKuykendall I was able to implement this solution.

 public void On_TOCItemDeleted(object Item)
 {
     var DeletedLayer = Item as ILayer;

     if (DeletedLayer == null)
       throw new Exception("Not iLayer file");


     var grouplyr = GetGroupByName(ArcMap.Document.FocusMap.Layers, groupname);

      grouplyr.Add(DeletedLayer);

     ArcMap.Document.UpdateContents();
     ArcMap.Document.ActiveView.Refresh();
}

The 'object' passed to the method is the layer being deleted, so I can just re-add to the TOC.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.