About the Simple logging dockable window with a custom context menu Sample
[C#]
LogLineMultiItemCmd.cs
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using ESRI.ArcGIS.ADF.CATIDs; using ESRI.ArcGIS.SystemUI; using ESRI.ArcGIS.Framework; using ESRI.ArcGIS.ArcMapUI; using ESRI.ArcGIS.esriSystem; namespace SimpleLogWindowCS { [Guid("21532172-bc21-43eb-a2ad-bb6c333eff5e")] [ClassInterface(ClassInterfaceType.None)] [ProgId("SimpleLogWindowCS.LogLineMultiItemCmd")] public class LogLineMultiItemCmd : IMultiItem { #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); GxCommands.Register(regKey); GMxCommands.Register(regKey); MxCommands.Register(regKey); SxCommands.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); GxCommands.Unregister(regKey); GMxCommands.Unregister(regKey); MxCommands.Unregister(regKey); SxCommands.Unregister(regKey); } #endregion #endregion private System.Windows.Forms.ListBox m_targetListBox; #region "IMultiItem Implementations" public string Caption { get { return "Delete log by line (C#)"; } } public int HelpContextID { get { return default(int); } } public string HelpFile { get { return default(string); } } public string Message { get { return "Delete a specific line in the simple log dockable window"; } } public string Name { get { return "CSNETSamples_DeleteLogLineCommand"; } } public void OnItemClick(int index) { if (index > -1) m_targetListBox.Items.RemoveAt(index); //Delete the line } public int OnPopup(object hook) { IApplication app = hook as IApplication; //This command is designed to be on a context menu displayed when the //logging window is right-clicked. Get the context item of the application IDocument doc = app.Document; object contextItem = null; if (doc is IBasicDocument) { contextItem = ((IBasicDocument)doc).ContextItem; } IDockableWindow dockWin = null; UID logWindowID = new UIDClass(); logWindowID.Value = "{600cb3c8-e9d8-4c20-b2c7-f97082b10f92}"; if (contextItem != null && contextItem is IDockableWindow) { dockWin = (IDockableWindow)contextItem; } else //In the case of ArcCatalog or the command has been placed outside the designated context menu { //Get the dockable window directly IDockableWindowManager dockWindowManager = (IDockableWindowManager)app; dockWin = dockWindowManager.GetDockableWindow(logWindowID); } //Get list item count in the dockable window if (dockWin != null && dockWin.ID.Compare(logWindowID)) { m_targetListBox = dockWin.UserData as System.Windows.Forms.ListBox; return m_targetListBox.Items.Count; } return 0; //failed or not applicable } public int get_ItemBitmap(int index) { return 0; } public string get_ItemCaption(int index) { if (index > -1) { string formatMessage = m_targetListBox.Items[index].ToString(); if (formatMessage.Length > 25) //Trim display string { formatMessage = formatMessage.Substring(0, 11) + "..." + formatMessage.Substring(formatMessage.Length - 11); } return string.Format("Delete line {0}: {1}", index + 1, formatMessage); } return ""; } public bool get_ItemChecked(int index) { return false; } public bool get_ItemEnabled(int index) { return true; } #endregion } }
[Visual Basic .NET]
LogLineMultiItemCmd.vb
Imports ESRI.ArcGIS.ADF.CATIDs Imports ESRI.ArcGIS.SystemUI Imports System.Runtime.InteropServices Imports ESRI.ArcGIS.Framework Imports ESRI.ArcGIS.esriSystem Imports ESRI.ArcGIS.ArcMapUI <ComClass(LogLineMultiItemCmd.ClassId, LogLineMultiItemCmd.InterfaceId, LogLineMultiItemCmd.EventsId), _ ProgId("SimpleLogWindowVB.LogLineMultiItemCmd")> _ Public Class LogLineMultiItemCmd Implements ESRI.ArcGIS.SystemUI.IMultiItem #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" ''' <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) GxCommands.Register(regKey) GMxCommands.Register(regKey) MxCommands.Register(regKey) SxCommands.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) GxCommands.Unregister(regKey) GMxCommands.Unregister(regKey) MxCommands.Unregister(regKey) SxCommands.Unregister(regKey) End Sub #End Region #End Region #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 = "a0e848c1-b873-4604-b4c0-3e4b6a901c79" Public Const InterfaceId As String = "aba47356-846b-4770-901f-e424d11f91d9" Public Const EventsId As String = "e8c98041-1026-4c02-be21-8071ff1397e5" #End Region Private m_targetListBox As System.Windows.Forms.ListBox ' 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() End Sub Public ReadOnly Property Caption() As String Implements ESRI.ArcGIS.SystemUI.IMultiItem.Caption Get Return "Delete log by line (VB.Net)" End Get End Property Public ReadOnly Property HelpContextID() As Integer Implements ESRI.ArcGIS.SystemUI.IMultiItem.HelpContextID Get Return 0 End Get End Property Public ReadOnly Property HelpFile() As String Implements ESRI.ArcGIS.SystemUI.IMultiItem.HelpFile Get Return "" End Get End Property Public ReadOnly Property ItemBitmap(ByVal index As Integer) As Integer Implements ESRI.ArcGIS.SystemUI.IMultiItem.ItemBitmap Get Return 0 End Get End Property Public ReadOnly Property ItemCaption(ByVal index As Integer) As String Implements ESRI.ArcGIS.SystemUI.IMultiItem.ItemCaption Get If index > -1 Then Dim formatMessage As String = m_targetListBox.Items(index) If formatMessage.Length > 25 Then 'Trim display string formatMessage = formatMessage.Substring(0, 11) + "..." + formatMessage.Substring(formatMessage.Length - 11) End If Return String.Format("Delete line {0}: {1}", index + 1, formatMessage) End If Return "" End Get End Property Public ReadOnly Property ItemChecked(ByVal index As Integer) As Boolean Implements ESRI.ArcGIS.SystemUI.IMultiItem.ItemChecked Get Return False End Get End Property Public ReadOnly Property ItemEnabled(ByVal index As Integer) As Boolean Implements ESRI.ArcGIS.SystemUI.IMultiItem.ItemEnabled Get Return True End Get End Property Public ReadOnly Property Message() As String Implements ESRI.ArcGIS.SystemUI.IMultiItem.Message Get Return "Delete a specific line in the simple log dockable window" End Get End Property Public ReadOnly Property Name() As String Implements ESRI.ArcGIS.SystemUI.IMultiItem.Name Get Return "VBNETSamples_DeleteLogLineCommand" End Get End Property Public Sub OnItemClick(ByVal index As Integer) Implements ESRI.ArcGIS.SystemUI.IMultiItem.OnItemClick If index > -1 Then m_targetListBox.Items.RemoveAt(index) 'Delete the line End If End Sub Public Function OnPopup(ByVal hook As Object) As Integer Implements ESRI.ArcGIS.SystemUI.IMultiItem.OnPopup Dim app As IApplication = DirectCast(hook, IApplication) 'This command is designed to be on a context menu displayed when the 'logging window is right-clicked. Get the context item of the application Dim doc As IDocument = app.Document Dim contextItem As Object If TypeOf doc Is IBasicDocument Then contextItem = DirectCast(doc, IBasicDocument).ContextItem End If Dim dockWin As IDockableWindow Dim logWindowID As New UIDClass() logWindowID.Value = "{8582b32d-120c-407b-af34-8719b8960b30}" If contextItem IsNot Nothing AndAlso TypeOf contextItem Is IDockableWindow Then dockWin = DirectCast(contextItem, IDockableWindow) Else 'In the case of ArcCatalog or the command has been placed outside the designated context menu 'Get the dockable window directly Dim dockWindowManager As IDockableWindowManager = DirectCast(app, IDockableWindowManager) dockWin = dockWindowManager.GetDockableWindow(logWindowID) End If 'Get list item count in the dockable window If dockWin IsNot Nothing AndAlso dockWin.ID.Compare(logWindowID) Then m_targetListBox = DirectCast(dockWin.UserData, System.Windows.Forms.ListBox) Return m_targetListBox.Items.Count End If Return 0 'failed or not applicable End Function End Class