ArcObjects Library Reference  

LayerSelectable

About the Displaying a TOCControl context menu Sample

[C#]

LayerSelectable.cs

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

namespace ContextMenu
{
	public sealed class LayerSelectable : BaseCommand, ICommandSubType
	{
		private IMapControl3 m_mapControl;
		private long m_subType;

		public LayerSelectable()
		{
		}
	
		public override void OnClick()
		{
			IFeatureLayer layer = (IFeatureLayer) m_mapControl.CustomProperty;
			if (m_subType == 1)	layer.Selectable = true;
			if (m_subType == 2) layer.Selectable = false;
		}
	
		public override void OnCreate(object hook)
		{
			m_mapControl = (IMapControl3) hook;
		}
		
		public override bool Enabled
		{
			get
			{
				ILayer layer = (ILayer) m_mapControl.CustomProperty;
				if (layer is IFeatureLayer)
				{
					IFeatureLayer featureLayer = (IFeatureLayer) layer;
					if (m_subType == 1) return !featureLayer.Selectable;
					else return featureLayer.Selectable;
				}
				else
				{
					return false;
				}
			}
		}
	
		public int GetCount()
		{
			return 2;
		}
	
		public void SetSubType(int SubType)
		{
			m_subType = SubType;
		}
	
		public override string Caption
		{
			get
			{
				if (m_subType == 1) return "Layer Selectable";
				else  return "Layer Unselectable";
			}
		}
	}
}


[Visual Basic .NET]

LayerSelectable.vb

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

Public NotInheritable Class LayerSelectable

    Inherits BaseCommand
    Implements ICommandSubType

    Private m_pMapControl As IMapControl3
    Private m_lSubType As Long

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

    Public Overrides Sub OnCreate(ByVal hook As Object)
        m_pMapControl = hook
    End Sub

    Public Overrides Sub OnClick()
        Dim pLayer As IFeatureLayer
        pLayer = m_pMapControl.CustomProperty
        If (m_lSubType = 1) Then pLayer.Selectable = True
        If (m_lSubType = 2) Then pLayer.Selectable = False
    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 Enabled() As Boolean
        Get
            Dim pLayer As ILayer
            pLayer = m_pMapControl.CustomProperty

            If (TypeOf pLayer Is IFeatureLayer) Then
                Dim pFeatureLayer As IFeatureLayer
                pFeatureLayer = pLayer
                If (m_lSubType = 1) Then Return Not pFeatureLayer.Selectable
                If (m_lSubType = 2) Then Return pFeatureLayer.Selectable
            Else
                Return False
            End If
        End Get
    End Property

    Public Overrides ReadOnly Property Caption() As String
        Get
            If (m_lSubType = 1) Then
                Return "Layer Selectable"
            Else
                Return "Layer Unselectable"
            End If
        End Get
    End Property
End Class