AnsweredAssumed Answered

Enabled property when implementing basecommand

Question asked by ginomellino on Apr 25, 2012
Latest reply on Apr 29, 2012 by ginomellino
Hi All,

I am wondering if someone could help me solve this (I assume very simple) problem. I just need to know how to dynamically set the Enabled property of a basecommand? I can set it when ArcMap first starts in the OnCreate event but I need to set it whilst working. I assume there is some event which is fired when I click on the menu that contains the command, maybe this is where I need to put the If, Then, Else statement. For example, maybe I want to enable/disable the command based on whether or not a particular form is loaded. I used to be able to do it by implementing Icommand and then setting the enabled property but do not have much experience using Basecommand.

Any help is appreciated. My Code is below.

Cheers

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

<ComClass(DERMPAMappingTools_comAddShapefile.ClassId, DERMPAMappingTools_comAddShapefile.InterfaceId, DERMPAMappingTools_comAddShapefile.EventsId), _
ProgId("DERM_PA_Mapping_Tools.DERMPAMappingTools_comAddShapefile")> _
Public NotInheritable Class DERMPAMappingTools_comAddShapefile
    Inherits BaseCommand

#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 = "a9a0d8f2-12aa-40c6-aed4-debdfe7bc9f7"
    Public Const InterfaceId As String = "81defa60-182e-4b4a-94e5-6e4711c97ff7"
    Public Const EventsId As String = "5536dbe9-d12f-4ee7-be2a-2968f6685834"
#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

    ' 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()

        ' TODO: Define values for the public properties
        MyBase.m_category = "DERMPAMappingTools"  'localizable text
        MyBase.m_caption = "Add Shapefile"   'localizable text
        MyBase.m_message = "Add shapefile to ArcMap session"   'localizable text
        MyBase.m_toolTip = "Open dialog to locate and add shapefile to ArcMap session" 'localizable text
        MyBase.m_name = "DERMPAMappingTools_comAddShapefile"  'unique id, non-localizable (e.g. "MyCategory_ArcMapCommand")

        Try
            'TODO: change bitmap name if necessary
            Dim bitmapResourceName As String = Me.GetType().Name + ".bmp"
            MyBase.m_bitmap = New Bitmap(Me.GetType(), bitmapResourceName)
        Catch ex As Exception
            System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap")
        End Try

    End Sub


    Public Overrides Sub OnCreate(ByVal hook As Object)
        If Not hook Is Nothing Then
            g_pApp = CType(hook, IApplication)

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

        End If

        ' TODO:  Add other initialization code
        SetUpDocumentEvent()
    End Sub

    Public Overrides Sub OnClick()
        'TODO: Add DERMPAMappingTools_comAddShapefile.OnClick implementation
        AddShapefileUsingOpenFileDialog()
    End Sub

End Class

Outcomes