How to enable / disable a tool on the toolbar in ArcObjects?

5279
7
Jump to solution
05-22-2012 12:32 PM
ShaningYu
Frequent Contributor
In a typical ArcObjects project in C#, the created toolbar contains 3 or 4 tools.  I want set one of the tools disabled in the start.  How to do it?  Can anyone provide the hint?  Thanks.
0 Kudos
1 Solution

Accepted Solutions
AlexanderGray
Occasional Contributor III
The code provided by Ken is vb.net not vba, so for a c# add-in it is the same just with a c# syntax
http://www.harding.edu/fmccown/vbnet_csharp_comparison.html

if this is a not an add-in and is a tool inheriting basetool, you just have to override the Enabled property of the basetool and put whatever code you need in there as long as it returns true or false.  Hint, don't put code too elaborate in the enabled property because it runs basically all the time (ie on mouse move.)  If the tools or commands need to talk to notify each other, I suggest adding an extension to the mix and setting and reading values on the extension.  There can be more than one instance of a command or tool in arcmap which will lead to confusion over where the value is set.  There can only be one extension with the same classid loaded at one time making a unique repository for values that need to be passed around.

View solution in original post

0 Kudos
7 Replies
KenBuja
MVP Esteemed Contributor
If you're creating this as part of an Add-in, you have to make a modification to the config.esriaddinx file for each tool. You have to add the tag onDemand="False". See the section on Delay loading in the Help for more information

<Tool id="myProject_myTool" class="myTool" message="A tool" caption="A tool" tip=A tool" category="My Tools" image="Images\myTool.png" onDemand="false" />


In addition, you'll have to add the code in the tool itself to enable it, which would go in the OnUpdate sub

Public Class myTool
    Inherits ESRI.ArcGIS.Desktop.AddIns.Tool

    Public Sub New()

    End Sub

    Protected Overrides Sub OnUpdate()

        Me.Enabled = 'your enabling code

    End Sub
0 Kudos
ShaningYu
Frequent Contributor
Thanks for your response.  I coded in C# rather than in VBA.  Do you know how to do it?  Thanks again.
0 Kudos
AlexanderGray
Occasional Contributor III
The code provided by Ken is vb.net not vba, so for a c# add-in it is the same just with a c# syntax
http://www.harding.edu/fmccown/vbnet_csharp_comparison.html

if this is a not an add-in and is a tool inheriting basetool, you just have to override the Enabled property of the basetool and put whatever code you need in there as long as it returns true or false.  Hint, don't put code too elaborate in the enabled property because it runs basically all the time (ie on mouse move.)  If the tools or commands need to talk to notify each other, I suggest adding an extension to the mix and setting and reading values on the extension.  There can be more than one instance of a command or tool in arcmap which will lead to confusion over where the value is set.  There can only be one extension with the same classid loaded at one time making a unique repository for values that need to be passed around.
0 Kudos
KenBuja
MVP Esteemed Contributor
Take a look at this example code for a C# implementation.

Also, I notice that you post many questions, but do not mark the post that have answered your questions. By clicking the check mark (instead of using the promote arrow), you help others who are searching the posts to find the questions that have been successfully answered. Please review this blog posting about marking questions as answered.
0 Kudos
MiteshPatel
New Contributor
Hi! Ken Buja,
Thanks for posting reply. It worked for me. I have been searching for this solution. I tried to use OnUpdate() method, but did not know that I should set onDemand="false" in config file, and that is why my code was not working as expected.

Thanks once Again.

Mitesh Patel.
0 Kudos
MiteshPatel
New Contributor
If you're creating this as part of an Add-in, you have to make a modification to the config.esriaddinx file for each tool. You have to add the tag onDemand="False". See the section on Delay loading in the Help for more information

<Tool id="myProject_myTool" class="myTool" message="A tool" caption="A tool" tip=A tool" category="My Tools" image="Images\myTool.png" onDemand="false" />


In addition, you'll have to add the code in the tool itself to enable it, which would go in the OnUpdate sub

Public Class myTool
    Inherits ESRI.ArcGIS.Desktop.AddIns.Tool

    Public Sub New()

    End Sub

    Protected Overrides Sub OnUpdate()

        Me.Enabled = 'your enabling code

    End Sub



Hi! Ken Buja,
Thanks for posting reply. It worked for me. I have been searching for this solution. I tried to use OnUpdate() method, but did not know that I should set onDemand="false" in config file, and that is why my code was not working as expected.

Thanks once Again.

Mitesh Patel.
0 Kudos
KenBuja
MVP Esteemed Contributor
This subject is a little hard to find in the documentation. It's found in the Delay Loading section of this topic.
0 Kudos