0

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
  • Arcmap may not be ready to work with layers when the tool is being loaded (not fully baked). I would prefer to look at how the checkbox changed event affects the layer. Reading this it seems that the code is not an add-in but rather an extension is that right? What are you implementing in this class? There may be a more suitable place to check your state than New() – Michael Stimson Jun 10 '15 at 22:24
  • 1
    Best ask the obvious question here, when you are testing this is there only 1 layer in your map? The layer property of IMap is zero based, so if you have only 1 layer you should write pMap.Layer(0). – Hornbydd Jun 10 '15 at 22:40
  • I think I have figured it out. I declared the pMap and pMxDoc variables as Private just before the Sub New and it seems to be working as intended now. – jencarta Jun 11 '15 at 12:40
  • Good to hear, can you supply an answer to your question and check it as complete. – artwork21 Jun 11 '15 at 12:43
0

My solution didn't quite work, but with the help of a friend, we created a solution that does work (mostly). We took the call to CheckStates() and AddItems() out of the Sub New and added the following code:

Private Sub OnOpenDocument()
    '-New CRl
    AddItems()
    CheckStates()
End Sub

AddItems() is a call to a subroutine that adds items to a combobox. This was working as expected when it was in Sub New, but we moved it out anyway. This code does check to see whether or not the basemap layer is visible, and if it is, it will place a check in the checkbox. It checks other layer properties as well and modifies the state of another checkbox and a slider. It works great if the tool window is already open when the MXD is loaded into ArcMap.

However, if the MXD is opened first and then the tool window opened, it does not work - which makes sense because it is listening for OnOpenDocument.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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