ArcObjects Library Reference  

LayerVisibility

About the Displaying a TOCControl context menu Sample

[C#]

LayerVisibility.cs

using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.SystemUI;

namespace ContextMenu
{
	public sealed class LayerVisibility : BaseCommand, ICommandSubType 
	{
		private IHookHelper m_hookHelper = new HookHelperClass();
		private long m_subType;

		public LayerVisibility()
		{
		}
	
		public override void OnClick()
		{
			for (int i=0; i <= m_hookHelper.FocusMap.LayerCount - 1; i++)
			{
				if (m_subType == 1) m_hookHelper.FocusMap.get_Layer(i).Visible = true;
				if (m_subType == 2) m_hookHelper.FocusMap.get_Layer(i).Visible = false;
			}
			m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography,null,null);
		}
	
		public override void OnCreate(object hook)
		{
			m_hookHelper.Hook = hook;
		}
	
		public int GetCount()
		{
			return 2;
		}
	
		public void SetSubType(int SubType)
		{
			m_subType = SubType;
		}
	
		public override string Caption
		{
			get
			{
				if (m_subType == 1) return "Turn All Layers On";
				else  return "Turn All Layers Off";
			}
		}
	
		public override bool Enabled
		{
			get
			{
				bool enabled = false; int i;
				if (m_subType == 1) 
				{
					for (i=0;i<=m_hookHelper.FocusMap.LayerCount - 1;i++)
					{
						if (m_hookHelper.ActiveView.FocusMap.get_Layer(i).Visible == false)
						{
							enabled = true;
							break;
						}
					}
				}
				else 
				{
					for (i=0;i<=m_hookHelper.FocusMap.LayerCount - 1;i++)
					{
						if (m_hookHelper.ActiveView.FocusMap.get_Layer(i).Visible == true)
						{
							enabled = true;
							break;
						}
					}
				}
				return enabled;
			}
		}
	}
}

[Visual Basic .NET]

LayerVisibility.vb

Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Controls
Imports ESRI.ArcGIS.ADF.BaseClasses
Imports ESRI.ArcGIS.SystemUI

Public NotInheritable Class LayerVisibility

    Inherits BaseCommand
    Implements ICommandSubType

    Private m_pHookHelper As New HookHelperClass
    Private m_lSubType As Long

    Public Sub New()
        MyBase.New()
    End Sub

    Public Overrides Sub OnCreate(ByVal hook As Object)
        m_pHookHelper.Hook = hook
    End Sub

    Public Overrides Sub OnClick()
        Dim i As Integer
        For i = 0 To m_pHookHelper.FocusMap.LayerCount - 1
            If (m_lSubType = 1) Then
                m_pHookHelper.FocusMap.Layer(i).Visible = True
            ElseIf (m_lSubType = 2) Then
                m_pHookHelper.FocusMap.Layer(i).Visible = False
            End If
        Next
        m_pHookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, Nothing, Nothing)
    End Sub

    Public Function GetCount() As Integer Implements ESRI.ArcGIS.SystemUI.ICommandSubType.GetCount
        Return 2
    End Function

    Public Sub SetSubType(ByVal SubType As Integer) Implements ESRI.ArcGIS.SystemUI.ICommandSubType.SetSubType
        m_lSubType = SubType
    End Sub

    Public Overrides ReadOnly Property Caption() As String
        Get
            If (m_lSubType = 1) Then
                Return "Turn All Layers On"
            Else
                Return "Turn All Layers Off"
            End If
        End Get
    End Property

    Public Overrides ReadOnly Property Enabled() As Boolean
        Get
            Dim i As Integer
            Dim bEnabled As Boolean

            bEnabled = False
            If (m_lSubType = 1) Then
                For i = 0 To m_pHookHelper.FocusMap.LayerCount - 1
                    If (m_pHookHelper.FocusMap.Layer(i).Visible = False) Then
                        bEnabled = True
                        Exit For
                    End If
                Next
            ElseIf (m_lSubType = 2) Then
                For i = 0 To m_pHookHelper.FocusMap.LayerCount - 1
                    If (m_pHookHelper.FocusMap.Layer(i).Visible = True) Then
                        bEnabled = True
                        Exit For
                    End If
                Next
            End If
            Return bEnabled
        End Get
    End Property
End Class