Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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