Controlled Deactivation of Add-In Tool using VB.net

1260
3
03-16-2012 09:59 AM
DavidFroehlich
New Contributor
I am using Visual Basic 2008 Express to develop a tool for correcting the locations of features. The user first selects the feature, then activates the tool and clicks on the correct location. The tool then starts edit mode and relocates the feature. At this point I want ArcMap to remain in edit mode until the user deactivates the tool, after which the user will be asked if he wants to save his edits. My problem is that I cannot figure out how to acquire control when the tool is deactivated.

The following simplified code illustrates the approach that I am trying to use. However, VB2008 gives the following error: "Interface 'ESRI.ArcGIS.SystemUI.ITool' is not implemented by this class". Can anyone tell me what I am doing wrong? 


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

Public Sub New()
MsgBox("Hello")
End Sub

Protected Overrides Sub OnUpdate()
Enabled = My.ArcMap.Application IsNot Nothing
End Sub

Public Function Deactivate() As Boolean Implements ESRI.ArcGIS.SystemUI.ITool.Deactivate
MsgBox("GoodBy")
Deactivate = True
End Function

End Class
0 Kudos
3 Replies
DavidFroehlich
New Contributor
I have found the solution to my own problem. I am new at using VB.net and I was trying to convert a program written in Visual Basic for Applications (VBA). In VBA, the following function call will allow you to run code when the tool is deactivated (my program was named MoveIt):

Private Function MoveIt_Deactivate() As Boolean
MoveSite_Deactivate = True
End Function

When I did a Google search for "arcgis deactivate tool", I found several programs with the following code:

Public Function Deactivate() As Boolean Implements ESRI.ArcGIS.SystemUI.ITool.Deactivate
' Deactivate the tool.
Return True
End Function

While this code may have worked in ArcGIS 8 and 9 (probably under VB 6), it no longer works in ArcGIS 10 and VB.net. However, the following program (which uses the function OnDeactivate) will work:

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

Public Sub New()
' happens only when the tool is first loaded
MsgBox("Hello New")
End Sub

Protected Overrides Sub OnUpdate()
Enabled = My.ArcMap.Application IsNot Nothing
End Sub

Protected Overrides Sub OnActivate()
MyBase.OnActivate()
' happens every time the tool is activated
MsgBox("Hello Activate")
End Sub

Protected Overrides Sub OnMouseDown(ByVal arg As ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs)
MyBase.OnMouseDown(arg)
' Do something to show that the tool is working
MsgBox("OnMouseDown - another mouse down")
End Sub

Protected Overrides Function OnDeactivate() As Boolean
Dim Response As String
Response = MsgBox("Do You really want to leave?", MsgBoxStyle.YesNo, "OnDeactivate")
If Response = vbYes Then
OnDeactivate = True
Else
OnDeactivate = False
End If
End Function

End Class

If you want to try this code, first start Visual Basic (I am using Visual Basic 2008 Express Edition) and under File/New Project create a new ArcMap Desktop Add-in. Include a tool in the project (it will probably be named Tool1 or ArcGISAddin1). Open the vb code window for the tool and delete everything but the first line (the line that starts with Public Class...). Then copy all of the above code except for the first line and paste it in your project (this ensures that the name of the tool will match the name in the Config.esriaddinx window). Then press the F5 key to start debugging and enter ArcGIS.

In ArcGIS open a map and enter the Customize Mode. Find your tool in the Add-IN Controls category (under the Commands tab) and drag it to a tool bar. Exit Customize Mode and click on the tool to activate it.
0 Kudos
LoganLehman
New Contributor
I wanted to thank you for this. I noticed there are a lot of things not documented, and this will help a lot of people. Good job!
0 Kudos
Ravichandran_M_Kaushika
Occasional Contributor
thank you for the highlight of the Deactivation problem.  it helped me fix it. 

Protected Function OnDeactivate() As Boolean
     'Do other units of work.



        Me.m_deactivate = True
        Me.m_enabled = False
    End Function


the compiler did not like the over ride word and the above function works.

thanks for your help.
0 Kudos