Select to view content in your preferred language

Enable command when extension is activated

1489
9
06-10-2010 11:34 AM
CarlosPiccirillo
Emerging Contributor
Hi everyone,

I am working on my first extension and have run into a problem. I am sure this problem is something simple, but I have been fighting with this for 2 days now and have looked at every example on the forums to do with extensions plus all the sample ESRI code I could find with no luck.

My extension works in conjuction with a custom toolbar. I have placed the code below in every command on the toolbar to disable it if the extension is turned off and this works fine. If the user turns on the extension first, then the toolbar, everything works fine but if the user turns on the toolbar first, then the extension, all of commands stay disabled.

What I think I need to do is activate the commands from the extension enabled code but can't figure out how to do this. All of my commands were created using the BaseCommand templates in C#.

Thanks,
Carlos


        public override void OnCreate(object hook)
        {
            if (hook == null)
                return;

            m_application = hook as IApplication;

            //Disable if it is not ArcMap
            if (hook is IMxApplication)
                base.m_enabled = true;
            else
                base.m_enabled = false;

            //Disable if RegGSS extension not turned on.
            UID pUID = new UIDClass();
            pUID.Value = "RegGSS.Extension";
            IExtensionConfig pExtension = m_application.FindExtensionByCLSID(pUID) as IExtensionConfig;

            if (pExtension == null)
                return;

            if (pExtension.State == esriExtensionState.esriESEnabled)
                base.m_enabled = true;
            else
                base.m_enabled = false;
        }
0 Kudos
9 Replies
KenBuja
MVP Esteemed Contributor
This is what I've done for my extension (in VB.NET). All the commands on the toolbar were created with BaseCommand and I overrode the Enabled property of each command.

  Public Overrides ReadOnly Property Enabled() As Boolean
    Get
      Dim ext As ESRI.ArcGIS.esriSystem.IExtension = m_application.FindExtensionByName("myExtension.Extension")
      Dim extConf As ESRI.ArcGIS.esriSystem.IExtensionConfig = ext

      If extConf.State = ESRI.ArcGIS.esriSystem.esriExtensionState.esriESEnabled Then
        Return True
      Else
        Return False
      End If
    End Get
  End Property
0 Kudos
CarlosPiccirillo
Emerging Contributor
Ken,

Thanks for the reply. Unless I am not understanding correctly, your code is doing the same thing mine is doing, disabling the commands on the toolbar if the extension is turned off. My problem is if the user turns on the toolbar first and then the extension, the commands on the toolbar stay disabled because the code to enable/disable the command in on the "on create" for the command and since the toolbar is already on, the "on create" code does not excute again. I was thinking I could refresh the command somehow but can't figure out how to do that.

Thanks again for your help!
Carlos
0 Kudos
KenBuja
MVP Esteemed Contributor
As you state, OnCreate is only fired when the command is created, whereas Enabled is fired continuously (which is also why you shouldn't have any extensive code in that subroutine). This will react to any changes in the environment, like enabling the extension.
0 Kudos
CarlosPiccirillo
Emerging Contributor
Ken,

Thanks for the reply. I think I understand what you are saying and I will give it try. Do you have a code example where you are using the previous code you sent me so I can see how to set it up properly?

Thanks again for your help.
Carlos

As you state, OnCreate is only fired when the command is created, whereas Enabled is fired continuously (which is also why you shouldn't have any extensive code in that subroutine). This will react to any changes in the environment, like enabling the extension.
0 Kudos
CarlosPiccirillo
Emerging Contributor
Hi Ken,

Got it working with your code. Thanks a bunch! Here's the code in C# in case in helps someone else.

        public override bool Enabled
        {
            get
            {
                UID pUID = new UIDClass();
                pUID.Value = "RegGSS.Extension";
                IExtensionConfig pExtension = m_application.FindExtensionByCLSID(pUID) as IExtensionConfig;

                if (pExtension == null)
                    return false;

                if (pExtension.State == esriExtensionState.esriESEnabled)
                    return true;
                else
                    return false;
            }
        }
0 Kudos
KirkKuykendall
Deactivated User
Unhandled exceptions in the Enabled getter can be a real pain.  I'd recommend a try/catch, and call the FindExtension in  OnCreate.  The enabled would return false if the extension reference is null.
0 Kudos
CarlosPiccirillo
Emerging Contributor
Thanks Kirk,

I think I understand what you are saying. I will give it a try. Do you have an example I can follow?

Thanks,
Carlos
0 Kudos
KirkKuykendall
Deactivated User
If you show a form every time you catch an exception in the Enabled getter, arcmap can become unwieldy.

To avoid seeing a messagebox every 400 millisecs, set the extension to null the first time the exception is caught.

untested code:

private IExtensionConfig m_MyExtension;
public override void OnCreate(object hook)
{
    if (hook == null)
        return;

    m_application = hook as IApplication;
    if (m_application != null)
    {
        m_MyExtension = m_application.FindExtensionByName("My Extension") as IExtensionConfig;
    }
}
public override bool Enabled
{
    get
    {
        bool enabled = false;
        try
        {
            if (m_MyExtension != null
                && m_MyExtension.State == esriExtensionState.esriESEnabled)
                enabled = AMethodThatMightThrowAnException();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace);
            m_MyExtension = null;
        }
        return enabled;
    }
}

0 Kudos
CarlosPiccirillo
Emerging Contributor
Thanks Kirk, I will give it a try. In your code, what code should method AMethodThatMightThrowAnException contain?
0 Kudos