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