About the Custom selection extension Sample
[C#]
SelectionTargetComboBox.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Desktop.AddIns;
namespace SelectionSample
{
public class SelectionTargetComboBox : ESRI.ArcGIS.Desktop.AddIns.ComboBox
{
private static SelectionTargetComboBox s_comboBox;
private int m_selAllCookie;
public SelectionTargetComboBox()
{
m_selAllCookie = -1;
s_comboBox = this;
}
internal static SelectionTargetComboBox GetSelectionComboBox()
{
return s_comboBox;
}
internal void AddItem(string itemName, IFeatureLayer layer)
{
if (s_comboBox.items.Count == 0)
{
m_selAllCookie = s_comboBox.Add("Select All");
s_comboBox.Select(m_selAllCookie);
}
// Add each item to combo box.
int cookie = s_comboBox.Add(itemName, layer);
}
internal void ClearAll()
{
m_selAllCookie = -1;
s_comboBox.Clear();
}
protected override void OnUpdate()
{
this.Enabled = SelectionExtension.IsExtensionEnabled();
}
protected override void OnSelChange(int cookie)
{
if (cookie == -1)
return;
foreach (ComboBox.Item item in this.items)
{
// All feature layers are selectable if "Select All" is selected;
// otherwise, only the selected layer is selectable.
IFeatureLayer fl = item.Tag as IFeatureLayer;
if (fl == null)
continue;
if (cookie == item.Cookie)
{
fl.Selectable = true;
continue;
}
fl.Selectable = (cookie == m_selAllCookie)? true : false;
}
// Fire ContentsChanged event to cause TOC to refresh with new selected layers.
ArcMap.Document.ActiveView.ContentsChanged(); ;
}
}
}
[Visual Basic .NET]
SelectionTargetComboBox.vb
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.IO
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Desktop.AddIns
Imports My
Namespace SelectionSample
Public Class SelectionTargetComboBox
Inherits ESRI.ArcGIS.Desktop.AddIns.ComboBox
Private Shared s_comboBox As SelectionTargetComboBox
Private m_selAllCookie As Integer
Public Sub New()
m_selAllCookie = -1
s_comboBox = Me
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 s_comboBox.items.Count = 0 Then
m_selAllCookie = s_comboBox.Add("Select All")
s_comboBox.Select(m_selAllCookie)
End If
' Add each item to combo box.
Dim cookie As Integer = s_comboBox.Add(itemName, layer)
End Sub
Friend Sub ClearAll()
m_selAllCookie = -1
s_comboBox.Clear()
End Sub
Protected Overrides Sub OnUpdate()
Me.Enabled = SelectionExtension.IsExtensionEnabled()
End Sub
Protected Overrides Sub OnSelChange(ByVal cookie As Integer)
If cookie = -1 Then
Return
End If
For Each item As ComboBox.Item In Me.items
' All feature layers are selectable if "Select All" is selected;
' otherwise, only the selected layer is selectable.
Dim fl As IFeatureLayer = TryCast(item.Tag, IFeatureLayer)
If fl Is Nothing Then
Continue For
End If
If cookie = item.Cookie 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.
ArcMap.Document.ActiveView.ContentsChanged()
End Sub
End Class
End Namespace