Thanks for the help. But I am creating a tool which can select multiple features eg. point features on a layer. I cannot use a query method. Please help.
A feature should have a key. So, if you have a key then you select using that key:
// filterClause is: "Key = <keyvalue>" or "Key in (<keyvaluelist>)"
// esriSelectionResultEnum lets you add or subtract from the current select (or create a new selection)
public static void SelectBy(IActiveView view, ILayer2 targetLayer, string filterClause, esriSelectionResultEnum selectionResult)
{
IFeatureSelection featureSelection = targetLayer as IFeatureSelection;
if (featureSelection == null) return;
IQueryFilter queryFilter = new QueryFilterClass();
queryFilter.WhereClause = filterClause;
IColor selectColor = Local.ColorToIColor(System.Drawing.Color.OrangeRed);
ISimpleFillSymbol fillSymbol = new SimpleFillSymbolClass();
fillSymbol.Color = selectColor;
ISimpleLineSymbol outlineSymbol = new SimpleLineSymbolClass();
outlineSymbol.Color = selectColor;
outlineSymbol.Width = 2;
fillSymbol.Outline = outlineSymbol;
fillSymbol.Style = esriSimpleFillStyle.esriSFSHollow;
featureSelection.SelectionSymbol = fillSymbol as ISymbol;
featureSelection.SetSelectionSymbol = true;
featureSelection.SelectFeatures(queryFilter, selectionResult, false);
view.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, view.FocusMap, null);
}
If you do not have a key and want to select by area then this selects from around a point:
// SelectByShape() will take any geometry (cricle, as here, or square, or rectangle)
public static IGeometry SelectFrom(IActiveView view, string limitToLayerName, IPoint point, float distance)
{
if (!string.IsNullOrEmpty(limitToLayerName)) view.SetLayerSelectable(limitToLayerName);
ISelectionEnvironment selectionEnvironment = null;
IGeometry geometry = null;
try
{
ICircularArc circularArc = new CircularArcClass();
IConstructCircularArc constructCircularArc = circularArc as IConstructCircularArc;
UnitConverter unitConverter = new UnitConverterClass();
double radius = unitConverter.ConvertUnits(
distance, esriUnits.esriFeet, view.FocusMap.DistanceUnits);
constructCircularArc.ConstructCircle(point, radius, false);
RingClass ring = new RingClass();
object missing = Type.Missing;
ring.AddSegment(circularArc as ISegment, ref missing, ref missing);
IPolygon polygon = new PolygonClass();
IGeometryCollection geometryCollection = polygon as IGeometryCollection;
geometryCollection.AddGeometry(ring, ref missing, ref missing);
geometry = polygon;
selectionEnvironment = new SelectionEnvironmentClass();
selectionEnvironment.CombinationMethod = esriSelectionResultEnum.esriSelectionResultNew;
IColor selectColor = selectionEnvironment.DefaultColor;
selectColor = Local.ColorToIColor(System.Drawing.Color.OrangeRed);
selectionEnvironment.DefaultColor = selectColor;
view.FocusMap.SelectByShape(geometry, selectionEnvironment, false);
view.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, view.Extent);
}
finally
{
Marshal.FinalReleaseComObject(selectionEnvironment);
}
return geometry;
}
If you are just clicking on features then get the key from the point and pass a filterClause containing the key to the SelectBy():
public static int GetTargetKeyByPoint(IMapControl4 mapControl, ILayer2 targetLayer, string keyName, IPoint point)
{
int targetKey = 0;
if (targetLayer == null) return targetKey;
ILayerFields parcelFields = targetLayer as ILayerFields;
if (parcelFields == null) return targetKey;
int keyIndex = parcelFields.FindField(keyName);
if (keyIndex == -1) return targetKey;
IIdentify identify = targetLayer as IIdentify;
IArray array = identify.Identify(point);
if (array != null && array.Count > 0)
{
IFeatureIdentifyObj featureIdentifyObj = array.get_Element(0) as IFeatureIdentifyObj;
IRowIdentifyObject rowIdentifyObject = featureIdentifyObj as IRowIdentifyObject;
IRow row = rowIdentifyObject.Row;
if (row != null)
{
IFeature feature = row as IFeature;
if (feature != null)
{
object value = feature.get_Value(keyIndex);
if (value != null)
{
targetKey = int.Parse(value.ToString());
}
}
}
}
return targetKey;
}