ISpatialFilter not selecting a polygon

Discussion created by DDW on Oct 20, 2011
Latest reply on Oct 20, 2011 by DDW
I am trying with the following code to use an xy generated from a mouse click to get the polygon's item value "Assessor number" and put that into a variable that I can use later in the code. Currently the code works with no errors, but I'm not having any success in getting any features selected by the filter. I tried to expand the filter by using an envelope, but still I have no features being placed into my cursor. I have noticed that the xy generated by the mouse click is in page units and maybe these need to be in map units to get it to work? If so how do I convert the xy? Here is the code..

ILayer layer = gxLayer.Layer;
string layerDirectory = @"M:\taxlots";
string layerFile = "parcels";
string tableDirectory = @"M:\informix";
string tableFile = "prty_nfo";

IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();
IWorkspace workspace = workspaceFactory.OpenFromFile(layerDirectory, 0);
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;
IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(layerFile);
ITable parcelTable = featureWorkspace.OpenTable(layerFile);
IWorkspaceFactory workspaceFactory2 = new ShapefileWorkspaceFactoryClass();
IWorkspace workspace2 = workspaceFactory2.OpenFromFile(tableDirectory, 0);
IFeatureWorkspace featureWorkspace2 = (IFeatureWorkspace)workspace2;
ITable prtyTable = featureWorkspace2.OpenTable(tableFile);

double searchTolerance = 25;
IPoint pPoint = new PointClass();
pPoint.X = x;
pPoint.Y = y;
IEnvelope envelope = pPoint.Envelope;
envelope.Expand(searchTolerance, searchTolerance, false);
ISpatialFilter pSFilter = new SpatialFilterClass();
pSFilter.Geometry = envelope;
pSFilter.GeometryField = featureClass.ShapeFieldName;
pSFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
pSFilter.SubFields = "ASSESSOR_N";

int nameFieldPos = featureClass.FindField("ASSESSOR_N");

IFeatureCursor featureCursor = featureClass.Search(pSFilter, false);
IFeature parcNumFeature = null;
while ((parcNumFeature = featureCursor.NextFeature()) != null)
[INDENT]                    string parcNum = Convert.ToString(parcNumFeature.get_Value(nameFieldPos));
                    MessageBox.Show(parcNum + ", Assessor Number");[/INDENT]

Any help or comments are greatly appreciated by this newbie to C#/ArcObjects programming!