ArcObjects Library Reference  

GraphicsLayersListCtrl

About the Graphics layers ToolControl Sample

[C#]

GraphicsLayersListCtrl.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.esriSystem;

namespace GraphicsLayerToolControl
{
  /// <summary>
  /// This user control hosts a combobox which allow the user to control over the active graphics layer 
  /// </summary>
  public partial class GraphicsLayersListCtrl : UserControl
  {
    #region class members
    IMap m_map = null;
    UID m_uid = null;
    #endregion

    #region class constructor
    public GraphicsLayersListCtrl()
    {
      InitializeComponent();

      //initialize the UID that will be used later to get the graphics layers
      m_uid = new UIDClass();
      m_uid.Value = "{34B2EF81-F4AC-11D1-A245-080009B6F22B}"; //graphics layers category      
    }
    #endregion

    /// <summary>
    /// Get the current map and wire the ActiveViewEvents
    /// </summary>
    public IMap Map
    {
      get { return m_map; }
      set 
      {
        m_map = value;
        if (null == m_map)
          return;

        //set verbose events in order to be able to listen to the various 'ItemXXX' events 
        ((IViewManager)m_map).VerboseEvents = true;

        //register document events in order to listen to layers which gets added or removed
        ((IActiveViewEvents_Event)m_map).ItemAdded += new IActiveViewEvents_ItemAddedEventHandler(OnItemAdded);
        ((IActiveViewEvents_Event)m_map).ItemReordered += new IActiveViewEvents_ItemReorderedEventHandler(OnItemReordered);
        ((IActiveViewEvents_Event)m_map).ItemDeleted += new IActiveViewEvents_ItemDeletedEventHandler(OnItemDeleted);

        //populate the combo with a list of the graphics layers
        PopulateCombo();
      }
    }

    /// <summary>
    /// occurs when the user select an item from the combo
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void cmbGraphicsLayerList_SelectedIndexChanged(object sender, EventArgs e)
    {
      if (null == m_map)
        return;

      //get the basic graphics layer from the map
      ILayer activeLayer = m_map.BasicGraphicsLayer as ILayer;
      //if the name of the selected item is the basic graphics layer, make it the active graphics layer
      if (activeLayer.Name == cmbGraphicsLayerList.SelectedItem.ToString())
      {
        m_map.ActiveGraphicsLayer = m_map.BasicGraphicsLayer as ILayer;
        return;
      }
      //iterate through the graphics layers
      IEnumLayer layers = GetGraphicsLayersList();
      if (null == layers)
        return;

      layers.Reset();
      ILayer layer = null;
      while ((layer = layers.Next()) != null)
      {
        if (layer is IGroupLayer)
          continue;

        if (layer is IGraphicsLayer)
        {
          //make the select item the active graphics layer
          if (layer.Name == cmbGraphicsLayerList.SelectedItem.ToString())
            m_map.ActiveGraphicsLayer = layer;
        }
      } 
    }

    /// <summary>
    /// get the list of all graphics layers in the map
    /// </summary>
    /// <returns></returns>
    private IEnumLayer GetGraphicsLayersList()
    {
      IEnumLayer layers = null;
      if (null == m_map || 0 == m_map.LayerCount)
        return null;

      try
      {
        layers = m_map.get_Layers(m_uid, true);
      }
      catch (Exception ex)
      {
        System.Diagnostics.Trace.WriteLine(ex.Message);
        return null;
      }

      return layers;
    }
    
    /// <summary>
    /// list the graphics layers in the combo and select the active graphics layer
    /// </summary>
    private void PopulateCombo()
    {
      if (null == m_map)
        return;

      //clear the items list of the combo
      cmbGraphicsLayerList.Items.Clear();

      //add the basic graphics layer name
      cmbGraphicsLayerList.Items.Add(((ILayer)m_map.BasicGraphicsLayer).Name);

      //get the active graphics layer
      ILayer activeLayer = m_map.ActiveGraphicsLayer;

      //get the list of all graphics layers in the map
      IEnumLayer layers = GetGraphicsLayersList();
      if (null != layers)
      {

        //add the layer names to the combo
        layers.Reset();
        ILayer layer = null;
        while ((layer = layers.Next()) != null)
        {
          cmbGraphicsLayerList.Items.Add(layer.Name);
        }
      }
      //set the selected item to be the active layer
      cmbGraphicsLayerList.SelectedItem = activeLayer.Name;
    }

    /// <summary>
    /// occurs when a layer is being deleted from the map
    /// </summary>
    /// <param name="Item"></param>
    void OnItemDeleted(object Item)
    {
      PopulateCombo();
    }

    /// <summary>
    /// occurs when a layer is being reordered in the TOC
    /// </summary>
    /// <param name="Item"></param>
    /// <param name="toIndex"></param>
    void OnItemReordered(object Item, int toIndex)
    {
      PopulateCombo();
    }

    /// <summary>
    /// occurs when a layer is being added to the map
    /// </summary>
    /// <param name="Item"></param>
    void OnItemAdded(object Item)
    {
      PopulateCombo();
    }
  }
}

[Visual Basic .NET]

GraphicsLayersListCtrl.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Text
Imports System.Windows.Forms
Imports ESRI.ArcGIS.Controls
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.SystemUI
Imports ESRI.ArcGIS.esriSystem

  ''' <summary>
  ''' This user control hosts a combobox which allow the user to control over the active graphics layer 
  ''' </summary>
Partial Public Class GraphicsLayersListCtrl : Inherits UserControl
#Region "class members"
  Private m_map As IMap = Nothing
  Private m_uid As UID = Nothing
#End Region

#Region "class constructor"
  Public Sub New()
    InitializeComponent()

    'initialize the UID that will be used later to get the graphics layers
    m_uid = New UIDClass()
    m_uid.Value = "{34B2EF81-F4AC-11D1-A245-080009B6F22B}" 'graphics layers category
  End Sub
#End Region

  ''' <summary>
  ''' Get the current map and wire the ActiveViewEvents
  ''' </summary>
  Public Property Map() As IMap
    Get
      Return m_map
    End Get
    Set(ByVal value As IMap)
      m_map = value
      If Nothing Is m_map Then
        Return
      End If

      'set verbose events in order to be able to listen to the various 'ItemXXX' events 
      CType(m_map, IViewManager).VerboseEvents = True

      'register document events in order to listen to layers which gets added or removed
      AddHandler (CType(m_map, IActiveViewEvents_Event)).ItemAdded, AddressOf OnItemAdded
      AddHandler (CType(m_map, IActiveViewEvents_Event)).ItemReordered, AddressOf OnItemReordered
      AddHandler (CType(m_map, IActiveViewEvents_Event)).ItemDeleted, AddressOf OnItemDeleted

      'populate the combo with a list of the graphics layers
      PopulateCombo()
    End Set
  End Property

  ''' <summary>
  ''' occurs when the user select an item from the combo
  ''' </summary>
  ''' <param name="sender"></param>
  ''' <param name="e"></param>
  Private Sub cmbGraphicsLayerList_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cmbGraphicsLayerList.SelectedIndexChanged
    If Nothing Is m_map Then
      Return
    End If

    'get the basic graphics layer from the map
    Dim activeLayer As ILayer = TryCast(m_map.BasicGraphicsLayer, ILayer)
    'if the name of the selected item is the basic graphics layer, make it the active graphics layer
    If activeLayer.Name = cmbGraphicsLayerList.SelectedItem.ToString() Then
      m_map.ActiveGraphicsLayer = TryCast(m_map.BasicGraphicsLayer, ILayer)
      Return
    End If
    'iterate through the graphics layers
    Dim layers As IEnumLayer = GetGraphicsLayersList()
    If Nothing Is layers Then
      Return
    End If

    layers.Reset()
    Dim layer As ILayer = layers.Next()

    Do While Not layer Is Nothing
      If TypeOf layer Is IGroupLayer Then
        Continue Do
      End If

      If TypeOf layer Is IGraphicsLayer Then
        'make the select item the active graphics layer
        If layer.Name = cmbGraphicsLayerList.SelectedItem.ToString() Then
          m_map.ActiveGraphicsLayer = layer
        End If
      End If
      layer = layers.Next()
    Loop
  End Sub

  ''' <summary>
  ''' get the list of all graphics layers in the map
  ''' </summary>
  ''' <returns></returns>
  Private Function GetGraphicsLayersList() As IEnumLayer
    Dim layers As IEnumLayer = Nothing
    If Nothing Is m_map OrElse 0 = m_map.LayerCount Then
      Return Nothing
    End If

    Try
      layers = m_map.Layers(m_uid, True)
    Catch ex As Exception
      System.Diagnostics.Trace.WriteLine(ex.Message)
      Return Nothing
    End Try

    Return layers
  End Function

  ''' <summary>
  ''' list the graphics layers in the combo and select the active graphics layer
  ''' </summary>
  Private Sub PopulateCombo()
    If Nothing Is m_map Then
      Return
    End If

    'clear the items list of the combo
    cmbGraphicsLayerList.Items.Clear()

    'add the basic graphics layer name
    cmbGraphicsLayerList.Items.Add((CType(m_map.BasicGraphicsLayer, ILayer)).Name)

    'get the active graphics layer
    Dim activeLayer As ILayer = m_map.ActiveGraphicsLayer

    'get the list of all graphics layers in the map
    Dim layers As IEnumLayer = GetGraphicsLayersList()
    If Not Nothing Is layers Then

      'add the layer names to the combo
      layers.Reset()
      Dim layer As ILayer = layers.Next()

      Do While Not layer Is Nothing
        cmbGraphicsLayerList.Items.Add(layer.Name)
        layer = layers.Next()
      Loop
    End If
    'set the selected item to be the active layer
    cmbGraphicsLayerList.SelectedItem = activeLayer.Name
  End Sub

  ''' <summary>
  ''' occurs when a layer is being deleted from the map
  ''' </summary>
  ''' <param name="Item"></param>
  Private Sub OnItemDeleted(ByVal Item As Object)
    PopulateCombo()
  End Sub

  ''' <summary>
  ''' occurs when a layer is being reordered in the TOC
  ''' </summary>
  ''' <param name="Item"></param>
  ''' <param name="toIndex"></param>
  Private Sub OnItemReordered(ByVal Item As Object, ByVal toIndex As Integer)
    PopulateCombo()
  End Sub

  ''' <summary>
  ''' occurs when a layer is being added to the map
  ''' </summary>
  ''' <param name="Item"></param>
  Private Sub OnItemAdded(ByVal Item As Object)
    PopulateCombo()
  End Sub
End Class