3

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
    Else
        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
    Try
        '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

2 Answers 2

4

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

0
4

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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