I have a checkbox control as part of an ArcMap add-in with a dockable window in VB .net that turns a particular layer’s visibility on and off in ArcMap. The initial state is set to “unchecked”. But, if I load the tool into ArcMap and use it when that layer is already visible, the checkbox works in reverse (checked when the layer is off and unchecked when the layer is on). Is there any way I can check the visibility of the layer when the tool is first loaded and then set the state of the checkbox?
I tried putting some code in Public Sub New() function for my dockable window, but this crashed ArcMap. Here is what I had (with 3 dots representing other code). My checkbox is named chkBasemap. In this simple example, there are always only 2 layers in my map. I just assume the basemap layer I want to toggle the visibility of is always the second layer. The first layer is a polygon layer.
Imports ESRI.ArcGIS.ArcMapUI
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.CartoUI
Imports ESRI.ArcGIS.Display
Imports ESRI.ArcGIS.Framework
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.esriSystem
Public Class DockWinClimateTools
Public Sub New(ByVal hook As Object)
InitializeComponent()
AddItems()
CheckStates()
Me.Hook = hook
End Sub
Private m_hook As Object
Public Property Hook() As Object
Get
Return m_hook
End Get
Set(ByVal value As Object)
m_hook = value
End Set
End Property
Public Class AddinImpl
Inherits ESRI.ArcGIS.Desktop.AddIns.DockableWindow
Private m_windowUI As DockWinClimateTools
Protected Overrides Function OnCreateChild() As System.IntPtr
m_windowUI = New DockWinClimateTools(Me.Hook)
Return m_windowUI.Handle
End Function
Protected Overrides Sub Dispose(ByVal Param As Boolean)
If m_windowUI IsNot Nothing Then
m_windowUI.Dispose(Param)
End If
End Sub
End Class
Private Sub chkBasemap_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkBasemap.CheckedChanged
Dim pMxDoc As IMxDocument = My.ArcMap.Application.Document
Dim pMap As IMap = pMxDoc.FocusMap
Dim pLayer As ILayer = pMap.Layer(1)
If pLayer.Visible Then
pLayer.Visible = False
Else
pLayer.Visible = True
End If
pMxDoc.ActiveView.ContentsChanged()
pMxDoc.ActiveView.Refresh()
End Sub
Public Sub CheckStates()
Dim pMxDoc As IMxDocument = My.ArcMap.Application.Document
Dim pMap As IMap = pMxDoc.FocusMap
Dim baseLayer As ILayer = pMap.Layer(1)
If baseLayer.Visible Then
chkBasemap.Checked = True
Else
chkBasemap.Checked = False
End If
End Sub
End Class