ILabelEngineLayerProperties not working

980
1
11-18-2011 05:49 AM
PLadd
by
Occasional Contributor III
Like many others, I am trying to use code to label selected features.  I've seen other thread discuss this issue and the solutions are the same but don't seem to work for me.

See here > http://forums.arcgis.com/threads/5664-displaying-labels-from-a-button-(vb.net)?highlight=LabelEngine...

And here > http://forums.arcgis.com/threads/5664-displaying-labels-from-a-button-(vb.net)?highlight=LabelEngine...

And here > http://forums.arcgis.com/threads/20019-set-expression-to-displayfield

I have the user select the field name from a combobox.  The selected feature still label, just with the default display field.  Can anybody see what I'm doing wrong?

//Get AnnotateLayerPropertiesCollection from Layer
                IAnnotateLayerPropertiesCollection pAnnoLayerPropsColl = pLayer.AnnotationProperties;
                IAnnotateLayerProperties pAnnoLayerProps;

                IElementCollection placedElements;
                IElementCollection unplacedElements;

                ElementCollection pEnumInVisibleElements = new ElementCollection();

                pAnnoLayerPropsColl.QueryItem(0, out pAnnoLayerProps, out placedElements, out unplacedElements);
                pAnnoLayerProps.Class = "LabelSel";
                pAnnoLayerProps.WhereClause = strSql;
                //display the labels
                ILabelEngineLayerProperties le = new LabelEngineLayerPropertiesClass();
                string exp = "[" + cbFieldNames.SelectedItem.ToString() + "]";
                le.Expression = exp;

                pLayer.DisplayAnnotation = true;
                //refresh the view
                pDoc.ActiveView.Refresh();
0 Kudos
1 Reply
PLadd
by
Occasional Contributor III
Problem solved.  Solution below for anybody else with same problem.

I made this change:
from this - ILabelEngineLayerProperties le = new LabelEngineLayerPropertiesClass();
to this - ILabelEngineLayerProperties le = pAnnoLayerProps as ILabelEngineLayerProperties;

And then learned that if I remove the join, I can label on the fly using the selected field from the combobox.

So now I load the combobox in this fashion to include the joined fields and everything works with the join.

            pLayer = (IGeoFeatureLayer)pDoc.SelectedLayer;
            pfc = pLayer.FeatureClass;

//added this to display joined fields           
IDisplayTable pDisplayTable = (IDisplayTable)pLayer;

            //create list of field names to pick one to label
            //IFields pFields = pfc.Fields;
            IFields pFields = pDisplayTable.DisplayTable.Fields;
            IField subField;
           
            for (int i = 0; i <= pFields.FieldCount - 1; i++)
            {
                subField = pFields.Field;
                cbFieldNames.Items.Add(subField.Name);
            }
0 Kudos