ArcObjects Library Reference  

RecentFilesCombo

Recently used filesóCommand, MultiItem, and ComboBox

[C#]

RecentFilesCombo.cs

using System;
using System.IO;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.SystemUI;

namespace RecentFilesCommandsCS
{
  /// <summary>
  /// Command that works in ArcMap/Map/PageLayout, ArcScene/SceneControl
  /// or ArcGlobe/GlobeControl
  /// </summary>
  [Guid("39b803a3-4bbb-4099-b4d9-89273af3685d")]
  [ClassInterface(ClassInterfaceType.None)]
  [ProgId("RecentFilesCommandsCS.RecentFilesCombo")]
  public sealed class RecentFilesCombo : BaseCommand, IComboBox
  {
    #region COM Registration Function(s)
    [ComRegisterFunction()]
    [ComVisible(false)]
    static void RegisterFunction(Type registerType)
    {
      // Required for ArcGIS Component Category Registrar support
      ArcGISCategoryRegistration(registerType);
    }

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

    #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);
      GMxCommands.Register(regKey);
      MxCommands.Register(regKey);
      SxCommands.Register(regKey);
      ControlsCommands.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);
      GMxCommands.Unregister(regKey);
      MxCommands.Unregister(regKey);
      SxCommands.Unregister(regKey);
      ControlsCommands.Unregister(regKey);
    }

    #endregion
    #endregion

    private IApplication m_application;
    private System.Collections.Generic.Dictionary<int, string> m_itemMap;
    private string m_strWidth = @"c:\documents\map documents";

    public RecentFilesCombo()
    {
      m_itemMap = new System.Collections.Generic.Dictionary<int, string>();

      base.m_category = ".NET Samples"; 
      base.m_caption = "Recent Documents: ";  
      base.m_message = "Recent Documents"; 
      base.m_toolTip = "Recent Documents";
      base.m_name = "RecentDocsCombo";
    }

    #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)
    {
      IComboBoxHook comboHook = hook as IComboBoxHook;
      if (comboHook == null)
      {
        m_enabled = false;
        return;
      }

      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);
        }
      }   
    }

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

    #endregion

    #region IComboBox Members

    public int DropDownHeight
    {
      get { return 50; }
    }

    public string DropDownWidth
    {
      get { return m_strWidth; }
    }

    public bool Editable
    {
      get { return false; }
    }

    public string HintText
    {
      get { return "Select document"; }
    }

    public void OnEditChange(string editString)
    {
    }

    public void OnEnter()
    {
    }

    public void OnFocus(bool set)
    {
    }

    public void OnSelChange(int cookie)
    {
      string selectedPath = m_itemMap[cookie];
      m_application.OpenDocument(selectedPath);
    }

    public bool ShowCaption
    {
      get { return false; }
    }

    public string Width
    {
      get { return m_strWidth; }
    }

    #endregion

  }
}

[Visual Basic .NET]

RecentFilesCombo.vb

Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Drawing
Imports ESRI.ArcGIS.ADF.BaseClasses
Imports ESRI.ArcGIS.ADF.CATIDs
Imports ESRI.ArcGIS.Framework
Imports ESRI.ArcGIS.SystemUI


<ComClass(RecentFilesCombo.ClassId, RecentFilesCombo.InterfaceId, RecentFilesCombo.EventsId), _
 ProgId("RecentFilesCommandsVB.RecentFilesCombo")> _
Public NotInheritable Class RecentFilesCombo
  Inherits BaseCommand
  Implements IComboBox


#Region "COM GUIDs"
  ' These  GUIDs provide the COM identity for this class 
  ' and its COM interfaces. If you change them, existing 
  ' clients will no longer be able to access the class.
  Public Const ClassId As String = "3b7bd3f6-246d-4761-8996-aa1409281cef"
  Public Const InterfaceId As String = "e88f97f3-afc2-41a0-a6a0-6375f1a3d6cf"
  Public Const EventsId As String = "077db9cd-d208-4452-8266-26b4fbae5ff0"
#End Region

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

    'Add any COM registration code after the ArcGISCategoryRegistration() call

  End Sub

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

    'Add any COM unregistration code after the ArcGISCategoryUnregistration() call

  End Sub

#Region "ArcGIS Component Category Registrar generated code"
  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
  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_application As IApplication
  Private m_itemMap As System.Collections.Generic.Dictionary(Of Integer, String)
  Private m_strWidth As String = "c:\documents\map documents"

  ' A creatable COM class must have a Public Sub New() 
  ' with no parameters, otherwise, the class will not be 
  ' registered in the COM registry and cannot be created 
  ' via CreateObject.
  Public Sub New()
    MyBase.New()

    MyBase.m_category = ".NET Samples"
    MyBase.m_caption = "Recent Documents: "
    MyBase.m_message = "Recent Documents"
    MyBase.m_toolTip = "Recent Documents"
    MyBase.m_name = "RecentDocsCombo"

    m_itemMap = New System.Collections.Generic.Dictionary(Of Integer, String)()

  End Sub


  Public Overrides Sub OnCreate(ByVal hook As Object)
    Dim comboHook As IComboBoxHook = TryCast(hook, IComboBoxHook)
    If comboHook Is Nothing Then
      m_enabled = False
      Return
    End If

    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 Overrides Sub OnClick()
  End Sub

  Public ReadOnly Property DropDownHeight() As Integer Implements ESRI.ArcGIS.SystemUI.IComboBox.DropDownHeight
    Get
      Return 50
    End Get
  End Property

  Public ReadOnly Property DropDownWidth() As String Implements ESRI.ArcGIS.SystemUI.IComboBox.DropDownWidth
    Get
      Return m_strWidth
    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 "Select document"
    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
    Dim selectedPath As String = m_itemMap(cookie)
    m_application.OpenDocument(selectedPath)
  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 m_strWidth
    End Get
  End Property
End Class