ArcObjects Library Reference  

cmdRemoveLayer

About the ArcGIS Network Analyst extension Engine application Sample

[C#]

cmdRemoveLayer.cs

using System.Runtime.InteropServices;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;

// This command removes the selected layer from the map
namespace NAEngine
{
	[Guid("53399A29-2B65-48d5-930F-804B88B85A34")]
	[ClassInterface(ClassInterfaceType.None)]
	[ProgId("NAEngine.RemoveLayer")]
	public sealed class cmdRemoveLayer : ESRI.ArcGIS.ADF.BaseClasses.BaseCommand
	{
		private ESRI.ArcGIS.Controls.IMapControl3 m_mapControl;

		public cmdRemoveLayer()
		{
			base.m_caption = "Remove Layer";
		}

		public override void OnClick()
		{
			if (m_mapControl == null)
			{
				MessageBox.Show("Error: Map control is null for this command");
				return;
			}

			// Get the layer that was right-clicked on in the table of contents
			// m_MapControl.CustomProperty was set in frmMain.axTOCControl1_OnMouseDown
			var layer = m_mapControl.CustomProperty as ILayer;
			if (layer == null)
			{
				MessageBox.Show("Error: The selected layer was not set as the CustomProperty of the map control");
				return;
			}

			// Remove the selected layer, then redraw the map
			m_mapControl.Map.DeleteLayer(layer);
			m_mapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, layer.AreaOfInterest);
		}

		public override void OnCreate(object hook)
		{
			// The "hook" was set as a MapControl in formMain_Load
			m_mapControl = hook as ESRI.ArcGIS.Controls.IMapControl3;
		}
	}
}

[Visual Basic .NET]

cmdRemoveLayer.vb

Imports Microsoft.VisualBasic
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports ESRI.ArcGIS.Carto

' This command removes the selected layer from the map
Namespace NAEngine
	<Guid("53399A29-2B65-48d5-930F-804B88B85A34"), ClassInterface(ClassInterfaceType.None), ProgId("NAEngine.RemoveLayer")> _
	Public NotInheritable Class cmdRemoveLayer : Inherits ESRI.ArcGIS.ADF.BaseClasses.BaseCommand
		Private m_mapControl As ESRI.ArcGIS.Controls.IMapControl3

		Public Sub New()
			MyBase.m_caption = "Remove Layer"
		End Sub

		Public Overrides Sub OnClick()
			If m_mapControl Is Nothing Then
				MessageBox.Show("Error: Map control is null for this command")
				Return
			End If

			' Get the layer that was right-clicked on in the table of contents
			' m_MapControl.CustomProperty was set in frmMain.axTOCControl1_OnMouseDown
            Dim layer As ILayer = TryCast(m_mapControl.CustomProperty, ILayer)
			If layer Is Nothing Then
				MessageBox.Show("Error: The selected layer was not set as the CustomProperty of the map control")
				Return
			End If

			' Remove the selected layer, then redraw the map
			m_mapControl.Map.DeleteLayer(layer)
			m_mapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, Nothing, layer.AreaOfInterest)
		End Sub

		Public Overrides Sub OnCreate(ByVal hook As Object)
			' The "hook" was set as a MapControl in formMain_Load
			m_mapControl = TryCast(hook, ESRI.ArcGIS.Controls.IMapControl3)
		End Sub
	End Class
End Namespace