Enabled/Disable button in Addin

5280
3
09-16-2010 06:47 PM
MikeLouwrens
Occasional Contributor III
I'm busy converting a lot of my old DLLs into Add-Ins for ArcGIS 10

My old toolbars had some buttons that would be enabled or disabled based on whether certain things were found in the map or not, something along the lines of:

 
Public Overrides ReadOnly Property Enabled() As Boolean
Get
 
If pMxDocument.FocusMap.LayerCount = 0 Then
Return MyBase.Enabled = False
Else
Return MyBase.Enabled = True
End If
 
End Get
End Property


This doesn't work in the AddIn.  It looks like what I want should be within
 
Protected Overrides Sub OnUpdate()
Enabled = My.ArcMap.Application IsNot Nothing
End Sub
However it doesn't seem to automatically pick up when things change - I actually have to click on my button to have it Enable or Disable.

How can I get my button to Automatically Enable or Disable when certain criteria are met?

Thanks,
Mike.
0 Kudos
3 Replies
ScottDavis
Occasional Contributor
The only way that I've been able to make this work without having to click on them first is to set onDemand to False in the config file. I would love to know if someone else has a better way.
0 Kudos
BrianBottoms
New Contributor
I was just about to post the same question - thanks for the OnDemand hint, it works great.

Brian
0 Kudos
MikeLouwrens
Occasional Contributor III
The only way that I've been able to make this work without having to click on them first is to set onDemand to False in the config file. I would love to know if someone else has a better way.
It appears that the onDemand attribute is the way we're supposed to control it:

Indicates if it is a delay loading command. By default, the assemblies associated with Add-In buttons and tools are not loaded until the corresponding item on a toolbar or menu is clicked by the user. This behavior helps conserve application memory and other resources. Since the enabled state of an Add-In button or tool is controlled by the OnUpdate method within code, they will initially appear enabled. If you need tighter control over the initial enabled state of a button or tool, you will need to override the default behavior and force the item to load at startup by setting the attribute equal to false.
(from
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/001v/001v0000024p000000.htm)

Cheers,
Mike.
0 Kudos