How to enable and disable the BaseCommand? BaseCommand::m_enable is read-only.

924
2
12-12-2013 02:18 AM
SanajyJadhav
Occasional Contributor II
Hello,

I have creates one custom command, inheriting from BaseCommand. Initially, I want to disable it and lateron, depending on some condition,want to enable it.

I observe that, BaseCommand::m_enabled can be set only in BaseCommand::OnCreate event. At the other times, this property is read-only.

So, I would like to know if it is possible to set the m_enabled property in any method other than OnCreate. Any help is appreciated.

Thanks in advance,
S.
0 Kudos
2 Replies
swapnabhide
New Contributor
Hi Sanjay,

I think the information and code below will be useful. This is taken from the developers help.
-----------------------
When implementing ICommand to create a custom command, add some logic to the Enabled property to specify in what state the application should be in for the command to be enabled.  


The following example makes the command enabled only when there is at least one data layer loaded in ArcMap.


Dim m_pApp As IApplication      'ArcMap application

Private Property Get ICommand_Enabled() As Boolean
     Dim pMxDoc As IMxDocument
     Dim pLayerCount As Integer
     'pApp is set in OnCreate   
     Set pMxDoc = m_pApp.Document
     pLayerCount = pMxDoc.FocusMap.LayerCount
    
     If pLayerCount>  0 Then
         ICommand_Enabled = True
     Else
         ICommand_Enabled = False
     End If
End Property

Private Sub ICommand_OnCreate(ByVal hook As Object)
     ' The hook argument is a pointer to Application object.
     Set m_pApp = hook
End Sub
--------------------
Hope this is what you would like to do. You can modify the code as per your requirement.

Cheers,
Swapna.
0 Kudos
SanajyJadhav
Occasional Contributor II
Swapna,

Thanks for reply. I fixed the issue today only based on similar logic but a bit different. Basically, I wanted to control this property in some later stage of the application. So, I overrode this property.

Below is my code :


   public override void OnClick()
        {
            //some code for onclick handler.
        }

 public override bool Enabled
  {
            get
            {
                //if editing is started, then enable the command.
                return Common.GlobalConfig.IsEditingStarted;
   }
 }



Thanks once again.
0 Kudos