ArcObjects Library Reference  

SelectionTargetComboBox

About the Create a custom selection extension by extending ArcObjects Sample

[C#]

SelectionTargetComboBox.cs

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Carto;
using System.Collections.Generic;

namespace SelectionCOMSample
{
  /// <summary>
  /// Summary description for SelectionTargetComboBox.
  /// </summary>
  [Guid("80e577d6-1b27-4471-986e-c04050a0e2d8")]
  [ClassInterface(ClassInterfaceType.None)]
  [ProgId("SelectionCOMSample.SelectionTargetComboBox")]
  public sealed class SelectionTargetComboBox : BaseCommand, IComboBox
  {
    #region COM Registration Function(s)
    [ComRegisterFunction()]
    [ComVisible(false)]
    static void RegisterFunction(Type registerType)
    {
      // Required for ArcGIS Component Category Registrar support
      ArcGISCategoryRegistration(registerType);

      //
      // TODO: Add any COM registration code here
      //
    }

    [ComUnregisterFunction()]
    [ComVisible(false)]
    static void UnregisterFunction(Type registerType)
    {
      // Required for ArcGIS Component Category Registrar support
      ArcGISCategoryUnregistration(registerType);

      //
      // TODO: Add any COM unregistration code here
      //
    }

    #region ArcGIS Component Category Registrar generated code
    /// <summary>
    /// Required method for ArcGIS Component Category registration -
    /// Do not modify the contents of this method with the code editor.
    /// </summary>
    private static void ArcGISCategoryRegistration(Type registerType)
    {
      string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
      MxCommands.Register(regKey);

    }
    /// <summary>
    /// Required method for ArcGIS Component Category unregistration -
    /// Do not modify the contents of this method with the code editor.
    /// </summary>
    private static void ArcGISCategoryUnregistration(Type registerType)
    {
      string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
      MxCommands.Unregister(regKey);

    }

    #endregion
    #endregion

    private IMxDocument m_doc;
    private static SelectionTargetComboBox s_comboBox;
    private int m_selAllCookie;
    private IComboBoxHook m_comboBoxHook;
    private Dictionary<int, IFeatureLayer> m_list;

    public SelectionTargetComboBox()
    {
      base.m_category = "Developer Samples";
      base.m_caption = "Selection Target";
      base.m_message = "Select the selection target C#.";
      base.m_toolTip = "Select the selection target C#.";  
      base.m_name = "ESRI_SelectionCOMSample_SelectionTargetComboBox";

      m_selAllCookie = -1;
      s_comboBox = this;
      m_list = new Dictionary<int, IFeatureLayer>();
      try
      {
        base.m_bitmap = new Bitmap(GetType().Assembly.GetManifestResourceStream("SelectionCOMSample.Images.SelectionTargetComboBox.png"));
      }
      catch (Exception ex)
      {
        System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
      }
    }
    internal static SelectionTargetComboBox GetSelectionComboBox()
    {
      return s_comboBox;
    }
    internal void AddItem(string itemName, IFeatureLayer layer)
    {
      if (m_selAllCookie == -1)
        m_selAllCookie = m_comboBoxHook.Add("Select All");

      //Add each item to combo box.
      int cookie = m_comboBoxHook.Add(itemName);
      m_list.Add(cookie, layer);
    }

    internal void ClearAll()
    {
      m_selAllCookie = -1;
      m_comboBoxHook.Clear();
    }

    #region Overriden Class Methods

    /// <summary>
    /// Occurs when this command is created
    /// </summary>
    /// <param name="hook">Instance of the application</param>
    public override void OnCreate(object hook)
    {
      if (hook == null)
        return;

      m_comboBoxHook = hook as IComboBoxHook;

      IApplication application = m_comboBoxHook.Hook as IApplication;
      m_doc = application.Document as IMxDocument;

      //Disable if it is not ArcMap
      if (m_comboBoxHook.Hook is IMxApplication)
        base.m_enabled = true;
      else
        base.m_enabled = false;

    }

    /// <summary>
    /// Occurs when this command is clicked
    /// </summary>
    public override void OnClick()
    {
    }

    #endregion

    #region IComboBox Members

    public int DropDownHeight
    {
      get { return 4; }
    }

    public string DropDownWidth
    {
      get { return "WWWWWWWWWWWWWWWWW"; }
    }

    public bool Editable
    {
      get { return false; }
    }

    public string HintText
    {
      get { return "Choose a target layer."; }
    }

    public void OnEditChange(string editString)
    {
    }

    public void OnEnter()
    {
    }

    public void OnFocus(bool set)
    {
    }

    public void OnSelChange(int cookie)
    {
      if (cookie == -1)
        return;

      foreach (KeyValuePair<int, IFeatureLayer> item in m_list)
      {
        //All feature layers are selectable if "Select All" is selected;
        //otherwise, only the selected layer is selectable.
        IFeatureLayer fl = item.Value;
        if (fl == null)
          continue;

        if (cookie == item.Key)
        {
          fl.Selectable = true;
          continue;
        }

        fl.Selectable = (cookie == m_selAllCookie) ? true : false;
      }

      //Fire ContentsChanged event to cause TOC to refresh with new selected layers.
      m_doc.ActiveView.ContentsChanged(); ;

    }

    public bool ShowCaption
    {
      get { return false; }
    }

    public string Width
    {
      get { return "WWWWWWWWWWWWWX"; }
    }

    #endregion
  }
}

[Visual Basic .NET]

SelectionTargetComboBox.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports ESRI.ArcGIS.ADF.BaseClasses
Imports ESRI.ArcGIS.ADF.CATIDs
Imports ESRI.ArcGIS.Framework
Imports ESRI.ArcGIS.ArcMapUI
Imports ESRI.ArcGIS.SystemUI
Imports ESRI.ArcGIS.Carto
Imports System.Collections.Generic

Namespace SelectionCOMSample
  ''' <summary>
  ''' Summary description for SelectionTargetComboBox.
  ''' </summary>
  <Guid("80e577d6-1b27-4471-986e-c04050a0e2d8"), ClassInterface(ClassInterfaceType.None), ProgId("SelectionCOMSample.SelectionTargetComboBox")> _
  Public NotInheritable Class SelectionTargetComboBox
	  Inherits BaseCommand
    Implements IComboBox

#Region "COM Registration Function(s)"
    <ComRegisterFunction(), ComVisible(False)> _
    Private Shared Sub RegisterFunction(ByVal registerType As Type)
      ' Required for ArcGIS Component Category Registrar support
      ArcGISCategoryRegistration(registerType)

      '
      ' TODO: Add any COM registration code here
      ''
    End Sub

    <ComUnregisterFunction(), ComVisible(False)> _
    Private Shared Sub UnregisterFunction(ByVal registerType As Type)
      ' Required for ArcGIS Component Category Registrar support
      ArcGISCategoryUnregistration(registerType)

      '
      ' TODO: Add any COM unregistration code here
      ''
    End Sub

#Region "ArcGIS Component Category Registrar generated code"
    ''' <summary>
    ''' Required method for ArcGIS Component Category registration -
    ''' Do not modify the contents of this method with the code editor.
    ''' </summary>
    Private Shared Sub ArcGISCategoryRegistration(ByVal registerType As Type)
      Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
      MxCommands.Register(regKey)

    End Sub
    ''' <summary>
    ''' Required method for ArcGIS Component Category unregistration -
    ''' Do not modify the contents of this method with the code editor.
    ''' </summary>
    Private Shared Sub ArcGISCategoryUnregistration(ByVal registerType As Type)
      Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
      MxCommands.Unregister(regKey)

    End Sub

#End Region
#End Region

    Private m_doc As IMxDocument
    Private Shared s_comboBox As SelectionTargetComboBox
    Private m_selAllCookie As Integer
    Private m_comboBoxHook As IComboBoxHook
    Private m_list As Dictionary(Of Integer, IFeatureLayer)

    Public Sub New()
      MyBase.m_category = "Developer Samples"
      MyBase.m_caption = "Selection Target"
      MyBase.m_message = "Select the selection target VB.NET."
      MyBase.m_toolTip = "Select the selection target VB.NET."
      MyBase.m_name = "ESRI_SelectionCOMSample_SelectionTargetComboBox"

      m_selAllCookie = -1
      s_comboBox = Me
      m_list = New Dictionary(Of Integer, IFeatureLayer)()
      Try
        MyBase.m_bitmap = New Bitmap(Me.GetType().Assembly.GetManifestResourceStream("SelectionTargetComboBox.png"))
      Catch ex As Exception
        System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap")
      End Try
    End Sub
    Friend Shared Function GetSelectionComboBox() As SelectionTargetComboBox
      Return s_comboBox
    End Function
    Friend Sub AddItem(ByVal itemName As String, ByVal layer As IFeatureLayer)
      If m_selAllCookie = -1 Then
        m_selAllCookie = m_comboBoxHook.Add("Select All")
      End If

      'Add each item to combo box.
      Dim cookie As Integer = m_comboBoxHook.Add(itemName)
      m_list.Add(cookie, layer)
    End Sub

    Friend Sub ClearAll()
      m_selAllCookie = -1
      m_comboBoxHook.Clear()
    End Sub

#Region "Overridden Class Methods"

        ''' <summary>
        ''' Occurs when this command is created
        ''' </summary>
        ''' <param name="hook">Instance of the application</param>
        Public Overrides Sub OnCreate(ByVal hook As Object)
            If hook Is Nothing Then
                Return
            End If

            m_comboBoxHook = TryCast(hook, IComboBoxHook)

            Dim application As IApplication = TryCast(m_comboBoxHook.Hook, IApplication)
            m_doc = TryCast(application.Document, IMxDocument)

            'Disable if it is not ArcMap
            If TypeOf m_comboBoxHook.Hook Is IMxApplication Then
                MyBase.m_enabled = True
            Else
                MyBase.m_enabled = False
            End If

        End Sub

        ''' <summary>
        ''' Occurs when this command is clicked
        ''' </summary>
        Public Overrides Sub OnClick()
        End Sub

#End Region

#Region "IComboBox Members"
    Public ReadOnly Property DropDownHeight() As Integer Implements ESRI.ArcGIS.SystemUI.IComboBox.DropDownHeight
      Get
        Return 4
      End Get
    End Property

    Public ReadOnly Property DropDownWidth() As String Implements ESRI.ArcGIS.SystemUI.IComboBox.DropDownWidth
      Get
        Return "WWWWWWWWWWWWWWWWW"
      End Get
    End Property

    Public ReadOnly Property Editable() As Boolean Implements ESRI.ArcGIS.SystemUI.IComboBox.Editable
      Get
        Return False
      End Get
    End Property

    Public ReadOnly Property HintText() As String Implements ESRI.ArcGIS.SystemUI.IComboBox.HintText
      Get
        Return "Choose a target layer."
      End Get
    End Property

    Public Sub OnEditChange(ByVal editString As String) Implements ESRI.ArcGIS.SystemUI.IComboBox.OnEditChange

    End Sub

    Public Sub OnEnter() Implements ESRI.ArcGIS.SystemUI.IComboBox.OnEnter

    End Sub

    Public Sub OnFocus(ByVal [set] As Boolean) Implements ESRI.ArcGIS.SystemUI.IComboBox.OnFocus

    End Sub

    Public Sub OnSelChange(ByVal cookie As Integer) Implements ESRI.ArcGIS.SystemUI.IComboBox.OnSelChange
      If cookie = -1 Then
        Return
      End If

      For Each item As KeyValuePair(Of Integer, IFeatureLayer) In m_list
        'All feature layers are selectable if "Select All" is selected;
        'otherwise, only the selected layer is selectable.
        Dim fl As IFeatureLayer = item.Value
        If fl Is Nothing Then
          Continue For
        End If

        If cookie = item.Key Then
          fl.Selectable = True
          Continue For
        End If

        fl.Selectable = If((cookie = m_selAllCookie), True, False)
      Next item

      'Fire ContentsChanged event to cause TOC to refresh with new selected layers.
      m_doc.ActiveView.ContentsChanged()
    End Sub

    Public ReadOnly Property ShowCaption() As Boolean Implements ESRI.ArcGIS.SystemUI.IComboBox.ShowCaption
      Get
        Return False
      End Get
    End Property

    Public ReadOnly Property Width() As String Implements ESRI.ArcGIS.SystemUI.IComboBox.Width
      Get
        Return "WWWWWWWWWWWWWX"
      End Get
    End Property
#End Region

  End Class
End Namespace