Displaying a TOCControl context menu
LayerVisibility.vb
' Copyright 2012 ESRI
' 
' All rights reserved under the copyright laws of the United States
' and applicable international laws, treaties, and conventions.
' 
' You may freely redistribute and use this sample code, with or
' without modification, provided you include the original copyright
' notice and use restrictions.
' 
' See the use restrictions.
' 

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