Need help with FeatureSelection

303
3
Jump to solution
06-18-2012 09:17 AM
DaveCouture
New Contributor III
Can someone tell me what I'm doing wrong, please? I'm just trying to select the first layer for a selection, in ArcMap, and I'm using the SelectMapFeaturesByAttributeQuery snippet, as a starting point.



Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.Carto
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'MsgBox(Me.TextBox1.Text)
        Dim pMap = My.ArcMap.Document.ActiveView
        SelectMapFeaturesByAttributeQuery(pMap, pMap.Layer(0), "PID = '55144133'") <-- something is wrong here  
      End Sub


    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub

    Public Sub SelectMapFeaturesByAttributeQuery(ByVal activeView As IActiveView, ByVal featureLayer As IFeatureLayer, ByVal whereClause As System.String)

        If activeView Is Nothing OrElse featureLayer Is Nothing OrElse whereClause Is Nothing Then
            Return
        End If

        Dim featureSelection As IFeatureSelection = TryCast(featureLayer, IFeatureSelection) ' Dynamic Cast

        ' Set up the query
        Dim queryFilter As IQueryFilter = New QueryFilterClass
        queryFilter.WhereClause = whereClause

        ' Invalidate only the selection cache. Flag the original selection
        activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)

        ' Perform the selection
        featureSelection.SelectFeatures(queryFilter, esriSelectionResultEnum.esriSelectionResultNew, False)

        ' Flag the new selection
        activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)

    End Sub
End Class
0 Kudos
1 Solution

Accepted Solutions
AlexanderGray
Regular Contributor II
You pMap variable is not strongly typed, that means it gets the type of the variable you are passing in (an IActiveView.) 
This would work but is still bad:
dim pMap as IMap = My.ArcMap.Document.ActiveView


It is bad for several reasons.  1.  it won't work with Option strict on (which would flag the first problem.) 2. It won't work if you are in layout view in ArcMap, since in LayoutView, the ActiveView is a PageLayout (Map and PageLayout implement IActiveView. 

The document has a maps method which is a collection of the maps in your document (there may be more than one.)  You can pick the first but it may still be incorrect.  I like using the IMxDocument.FocusMap.  If you are in data view, you get the map loaded into view, if you are in layout mode, you get the map which is selected on the page layout.

View solution in original post

0 Kudos
3 Replies
LeoDonahue
Regular Contributor
When I look at MxDocument, and see what ActiveView returns, it says it returns an IActiveView.  Is that what you want?  I'm thinking probably not.

What Alexander was trying to hint to you in the other thread is that you need a way to get the layer from the TOC.

Convert this to VB code and you are good to go.  Then pass the returned featureLayer to your select function.

Create a variable of type IMap and assign it the value of MxDocument.getActiveView.getFocusmap (you'll need that for the getLayerByName function.)

FeatureLayer fl = (FeatureLayer) getLayerByName("cities");

ESRI Sample method...

    public FeatureLayer getLayerByName(String layerName) {
        FeatureLayer layer = null;
        try {
            for (int i = 0; i < iMap.getLayerCount(); i++) {
                if (iMap.getLayer(i).getName().equalsIgnoreCase(layerName)) {
                    layer = (FeatureLayer) iMap.getLayer(i);
                    break;
                }
            }
        } catch (AutomationException ae) {
            ae.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Returns the featurelayer
        return layer;
    }
0 Kudos
AlexanderGray
Regular Contributor II
You pMap variable is not strongly typed, that means it gets the type of the variable you are passing in (an IActiveView.) 
This would work but is still bad:
dim pMap as IMap = My.ArcMap.Document.ActiveView


It is bad for several reasons.  1.  it won't work with Option strict on (which would flag the first problem.) 2. It won't work if you are in layout view in ArcMap, since in LayoutView, the ActiveView is a PageLayout (Map and PageLayout implement IActiveView. 

The document has a maps method which is a collection of the maps in your document (there may be more than one.)  You can pick the first but it may still be incorrect.  I like using the IMxDocument.FocusMap.  If you are in data view, you get the map loaded into view, if you are in layout mode, you get the map which is selected on the page layout.

View solution in original post

0 Kudos
DaveCouture
New Contributor III
Thanks - it's working now!!
0 Kudos