ArcObjects – How to Change the Label Field in GeoFeatureLayer

arcobjectsc

I currently have a basic shapefile that's a map of the United States. It has the name of the states, populations, and other pieces of information that I can get the names of via the following code:

IFeatureLayer myFeatureLayer = myLayer as IFeatureLayer;
IFeatureClass myFeatureClass = myFeatureLayer.FeatureClass;

For(int I = 0; I < myFeatureClass.Fields.FieldCount; i++)
{
IField field = myFeatureClass.Fields.get_Field(i);
Console.writeLine(field.Name);
}

The first field in the featureClass (after FID and the shape) is STATE_NAME which obviously is the name of each state.

If I do the following:

IGeoFeatureLayer geoLayer = myLayer as IGeoFeatureLayer;
geoLayer.displayAnnotation = true;

The states are now labeled; however, what I'd like is the ability to show different labels. I know from my looping through the featureclass Field count there are properties such as population and elevation. How do I change the AnnotationProperties to have it display that information instead of the default?

Best Answer

its set in properties.Expression ...

public void annotateLayer(ILayer thisLayer, String geocode, double minScale, double maxScale, bool annotationsOn, bool showMapTips, RgbColor annotationLabelColor)
{
   IGeoFeatureLayer geoLayer = thisLayer as IGeoFeatureLayer;
   if (geoLayer != null)
   {
        geoLayer.DisplayAnnotation = annotationsOn;
        IAnnotateLayerPropertiesCollection propertiesColl = geoLayer.AnnotationProperties;
        IAnnotateLayerProperties labelEngineProperties = new LabelEngineLayerProperties() as IAnnotateLayerProperties;
        IElementCollection placedElements = new ElementCollectionClass();
        IElementCollection unplacedElements = new ElementCollectionClass();
        propertiesColl.QueryItem(0, out labelEngineProperties, out placedElements, out unplacedElements);
        ILabelEngineLayerProperties lpLabelEngine = labelEngineProperties as ILabelEngineLayerProperties;
        lpLabelEngine.Expression = geocode;
        lpLabelEngine.Symbol.Color = annotationLabelColor; 
        labelEngineProperties.AnnotationMinimumScale = minScale;
        labelEngineProperties.AnnotationMaximumScale = maxScale; 
        IFeatureLayer thisFeatureLayer = thisLayer as IFeatureLayer;
        IDisplayString displayString = thisFeatureLayer as IDisplayString;
        IDisplayExpressionProperties properties = displayString.ExpressionProperties;
        properties.Expression = geocode; //example: "[OWNER_NAME] & vbnewline & \"$\" & [TAX_VALUE]";
        thisFeatureLayer.ShowTips = showMapTips;
}
}
Related Question