This document is archived and information here might be outdated.  Recommended version.


Creating combo boxes (ArcObjects .NET 10.4 SDK)

Creating combo boxes


Summary
A combo box is the combination of a text box and a drop-down list box that enables users to select items from the list or enter a new value.
Combo boxes were introduced at ArcGIS 10. You no longer have to implement a ToolControl to create a combo box. This topic describes how to implement a custom combo box by using an add-in and by extending ArcObjects.

In this topic


Using add-ins

Creating an add-in is the easiest and recommended way to implement a custom combo box. For more information on using add-ins, see Building add-ins for ArcGIS for Desktop.

Extending ArcObjects

You can also use the classic Component Object Model (COM) extensibility approach to implement a custom combo box. In this case, create a COM command and implement IComboBox. You can create a command by implementing ICommand or by having your class inherit from BaseCommand. For more information, see Creating commands and tools.
When implementing IComboBox, you must specify the size of the combo box and the size of its drop-down window. You can optionally supply a hint text string that will appear as gray text in the combo box. This is typically a description of the data to be entered or an instruction to the user. Combo boxes can be flagged as editable or not editable; an editable combo box accepts keyboard input the same way as an edit box.
The following screen shot shows an example of a combo box:
Combo boxes behave differently than other command objects in that the hook parameter passed to OnCreate is not a reference to the host application but rather an IComboBoxHook reference. To access the application, use IComboBoxHook.Hook. Use the IComboBoxHook reference to manage items in the combo box. For example, to add items, use IComboBoxHook.Add.
Combo boxes use a cookie mechanism to manage their items. When an item is added, a cookie (integer) is returned to identify the entry. For example, to remove an item, you must specify the cookie. Similarly, when an item is selected, its cookie is passed to the client code. Combo box implementations typically map their cookies using a dictionary or some other collection type. The following code shows an example:
[C#]
private System.Collections.Generic.Dictionary < int, string > m_itemMap;

public override void OnCreate(object hook)
{
    IComboBoxHook comboHook=hook as IComboBoxHook;

    m_application=comboHook.Hook as IApplication;
    int cookie=0;
    foreach (string fileName in RecentFilesRegistryHelper.GetRecentFiles
        (m_application))
    {
        if (File.Exists(fileName))
        {
            //Add item to list.
            cookie=comboHook.Add(fileName);
            m_itemMap.Add(cookie, fileName);
        }
    }
}

public void OnSelChange(int cookie)
{
    string selectedPath=m_itemMap[cookie];
    m_application.OpenDocument(selectedPath);
}
[VB.NET]
Private m_itemMap As System.Collections.Generic.Dictionary(Of Integer, String)

Public Overrides Sub OnCreate(ByVal hook As Object)
Dim comboHook As IComboBoxHook=TryCast(hook, IComboBoxHook)

m_application=TryCast(comboHook.Hook, IApplication)
Dim cookie As Integer=0
For Each fileName As String In RecentFilesRegistryHelper.GetRecentFiles(m_application)
    If File.Exists(fileName) Then
        'Add item to list.
        cookie=comboHook.Add(fileName)
        m_itemMap.Add(cookie, fileName)
    End If
Next
End Sub

Public Sub OnSelChange(ByVal cookie As Integer) Implements ESRI.ArcGIS.SystemUI.IComboBox.OnSelChange
    Dim selectedPath As String=m_itemMap(cookie)
    m_application.OpenDocument(selectedPath)
End Sub


See Also:

Samples: