4

I'm trying to regenerate a Legend in a Pagelayout whenever the layers visibility are changed in the TocControl. What is the best way to catch when a layer visibility is changed?

4

Watch for the VisibilityChanged event on the feature layer that you want to track.

Something like the following (taken from here):

private ILayerEvents_Event pLayerEvents;

void mActiveViewEvents_ItemAdded(object Item)
{
    if (Item is ILayerEvents_Event)            
    {
        pLayerEvents = Item as ILayerEvents_Event;
        pLayerEvents.VisibilityChanged += new ILayerEvents_VisibilityChangedEventHandler(pLayerEvents_VisibilityChanged);
    }
}

void pLayerEvents_VisibilityChanged(bool currentState)
{
    //do something here
}

A little more information is available here and here.

The links provided above point to ArcGIS 9.x content. If you plan on using ArcGIS 10, documentation is available here.

  • 1
    ArcGIS 10 documentation is over this way: help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/… – Herb Sep 20 '10 at 14:28
  • The sample only workes on the root level layers, so the only way to catch change in visibility is by adding an event listener on every layer? – MathiasWestin Sep 20 '10 at 14:41
  • 1
    @Herb Thanks, updated. @MatthiasWestin As far as I know, that's correct. Since this is a layer-level event, each layer you want to track must be tracked individually. I "solved" this by creating a single method to track all layers, then point to that method when adding the event. – Michael Todd Sep 20 '10 at 15:06
  • @Michael-Todd Ok, I make a recursive method looping through the composite and group layers. The Sample code is incorrect the if-statement should check for ILayer or even better ILayerEvents_Event, best regards. – MathiasWestin Sep 20 '10 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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