I need help to change the selected feature color to red in C#

2 weeks ago
New Contributor

IFeatureLayer pFeatureLayer;

IFeatureCursor pFeatureCursor;
IQueryFilter pQueryFilter;
ESRI.ArcGIS.Geodatabase.IFeature pFeature;
pFeatureLayer = this.axMapControl1.Map.get_Layer(0) as IFeatureLayer;
if (pFeatureLayer.Name != "parcel")
pQueryFilter = new QueryFilterClass();

pQueryFilter.WhereClause = "cc_number='LEKMA'";

pFeatureCursor = pFeatureLayer.Search(pQueryFilter, true);
pFeature = pFeatureCursor.NextFeature();

IFeatureSelection featureSelection = pFeature as IFeatureSelection;

if (pFeature != null)
this.axMapControl1.Map.SelectFeature(pFeatureLayer, pFeature);
this.axMapControl1.Extent = pFeature.Shape.Envelope;

MessageBox.Show("Not found" + "LEKMA");

0 Kudos
1 Reply
Regular Contributor


At first you can't cast from IFeature to IFeatureSelection. You can cast  IFeatureLayer to IFeatureSelection.

So it would be:

IFeatureSelection featureSelection = pFeatureLayer as IFeatureSelection;

If you look at your layer Properties-Selection tab you will find options on showing selection:


The first one is ArcMap setting which changes all layers selection showing. I think you don't want to change all layers selection to red. If you do then you should use SelectionColor on IMapDescription2.

If you are interesting in third option then you should use SelectionColor on ILayerDescription.

More info about both cases here:

- ILayerDescription.SelectionColor Property (ArcObjects .NET 10.8 SDK) (arcgis.com)

If you want mark one special from selection you can use IGraphicsContainer to draw graphic element on top of your feature with color or shape you want. 

0 Kudos