Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to enable an add-in button on my toolbar if only one feature is selected from a specifc Layer. For example, when ArMap loads, the button should be disabled. If the user selects one feature from the AJ_RecommendedImprovementsPoints layer, then the button enables. Here's what I have, but it's not working at all. suggestions??

Protected Overrides Sub OnUpdate()
    'Get Transportation Improvement Projects
    Dim pFeatLyr As IFeatureLayer
    pFeatLyr = GetLayerByTOC("RV_ImprovementPoints")
    'Enable Button
    If GetFeatSelCount(pFeatLyr) = 1 Then
        Me.Enabled = True
        Me.Enabled = False
    End If
End Sub

Public Function GetFeatSelCount(ByVal pFeatLyr As IFeatureLayer) As Long
    'Returns # of selected featurues of feature layer as long integer
    'Initialize the required variables
    Dim pMxDoc As IMxDocument
    pMxDoc = My.ArcMap.Document
    Dim pMap As IMap
    pMap = pMxDoc.FocusMap
    Dim pFeatSel As IFeatureSelection
    pFeatSel = pFeatLyr
    Dim pFeatSelectionSet As ISelectionSet
    pFeatSelectionSet = pFeatSel.SelectionSet
    GetFeatSelCount = pFeatSelectionSet.Count
End Function

Public Function GetLayerByTOC(ByVal lyrName As String) As IFeatureLayer
    'This function finds a feature layer based on its TOC Name
        'Initialize the required variables
        Dim pMxDoc As IMxDocument
        pMxDoc = My.ArcMap.Document
        Dim pMap As IMap
        pMap = pMxDoc.FocusMap
        Dim lyrCntr As Integer
        Dim pFeatLyr As IFeatureLayer
        For lyrCntr = 0 To pMap.LayerCount - 1
            'Ensure that the layer is valid
            If pMap.Layer(lyrCntr).Valid = True Then
                'Ensure that the layer is a feature layer
                If TypeOf pMap.Layer(lyrCntr) Is IFeatureLayer Then
                    pFeatLyr = pMap.Layer(lyrCntr)
                    If UCase(pMap.Layer(lyrCntr).Name) = UCase(lyrName) Then
                        GetLayerByTOC = pFeatLyr
                        Exit Function
                    End If
                End If
            End If
        Next lyrCntr
    Catch ex As Exception
        'Layer not found. Show a message
        MsgBox("Layer " & lyrName & " not found !", vbCritical, "Error")
    End Try
End Function
share|improve this question

You need a function that returns the selection set count property on your layer. If the count is >0 than you can enable your button.

ISelectionSet.Count Property

share|improve this answer

Instead of checking for a selection directly in the button's OnUpdate routine, which runs very frequently, I would write an extension that uses event handlers to respond to selection events and caches the selection state of your layer, and then in the OnUpdate routine check the extension's cached state.

Of course this is probably overkill if there is only one button/command to be updated, but it would be good practice. See this sample (SelectionExtension) for an example as well as Add-in coding patterns for best practices.

share|improve this answer

I added in a function for getting the selection count...but I'm having one issue still. I would like to have the button be disabled when you open the map and only enabled when one feature is selected. Currently, it only is disabled after the first time you push the button. Suggestions?

See first post for new code!

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.