Select to view content in your preferred language

VB.Net custom tool crashes ArcMap

2880
7
06-03-2011 09:26 AM
KevinAndras
Emerging Contributor
I'm trying to implement a custom tool for an add-in.  I inserted the following snippet, which works fine:
        Dim UIDCls As ESRI.ArcGIS.esriSystem.UID = New ESRI.ArcGIS.esriSystem.UIDClass()
        ' id property of menu from Config.esriaddinx document
        UIDCls.Value = "esriArcMapUI.ZoomInTool"
        Dim commandItem As ESRI.ArcGIS.Framework.ICommandItem = TryCast(document.CommandBars.Find(UIDCls), ESRI.ArcGIS.Framework.ICommandItem)
        If commandItem Is Nothing Then
            Exit Sub
        End If
        My.ArcMap.Application.CurrentTool = commandItem

I then added a custom tool:
Add New Item>ArcGis>Extending ArcObjects>Base Tool. I called it (default) Tool1.vb.  It created a module allowing me to add code for mouse events, along with some COM GUIDs. 

When I try to replace the UIDCls value above (esriArcMapUI.ZoomInTool) to refer to my custom tool, Arcmap will crash. 
My project is called VBNET_Test, so I've tried referring to my tool as VBNET_Test.Tool1, VBNET_Test_Tool1; also tried giving the tool a MyBase.m_name and referring to that as Tool1_[m_name].  Everything I do causes ArcMap to crash.  The only way I can get ArcMap to continue, (to the Exit Sub line, because the tool's not recognized - CommandItem=nothing) is by adding the classID in the tool module ("{513f6529-78cf-4913-a86d-058985bbf4ed}").

My tool is not on a toolbar/command bar - but I've read others' experiences stating that this is not necessary.  I don't want it on a toolbar.  In my VBA project I had to have the tool on a toolbar.

Potentially related is my selection of disabling Com interop in the main project properties.  If I enable it I get an error and the code won't compile.  The error states:

Error 1 Cannot register assembly "C:\Users\Computer\Documents\Visual Studio 2008\Projects\VBNet_Test\VBNet_Test\bin\Debug\VBNet_Test.dll". Could not load file or assembly 'ESRI.ArcGIS.Desktop.AddIns, Version=10.0.0.0, Culture=neutral, PublicKeyToken=8fc3cc631e44ad86' or one of its dependencies. The system cannot find the file specified. VBNet_Test

Any help would be greatly appreciated.  I'm trying to migrate a huge VBA project over to VB.net- some things seem really easy but this has me stumped.
0 Kudos
7 Replies
maxsteinbrenner
Emerging Contributor
have you tried steppping through your code to see the line it crashes on?

if you haven't already done it; i suggest putting a breakpoint into your code very early (before it bombs) and then use the "step into" tool to go through line by line and figuring out specifically where it is breaking.
0 Kudos
AlexanderGray
Honored Contributor
try/catch blocks.  Any code which handles events, such as create, click or in the class constructor (new), mouse down, etc. should have try catch blocks.  You can pop the exception in a message box or write it to the trace.  It should help you to figure out what is wrong and hopefully prevent arcmap from crashing.
0 Kudos
KevinAndras
Emerging Contributor
Yes, I've done that. If I give the Clas ID ({513f6529-78cf-4913-a86d-058985bbf4ed}) then CommandItem ends up as Nothing and the code exits, without implementing my tool. If I tell it anything else (i.e. VBNET_Test.Tool1, VBNET_Test_Tool1, etc) Arc will crash on the same line:
UIDCls.Value = {whatever}
0 Kudos
KevinAndras
Emerging Contributor
If I catch the exception, this is the error message:
{"Value does not fall within the expected range."}
0 Kudos
KevinAndras
Emerging Contributor
I changed the UID value to this:
uid.Value = My.ThisAddIn.IDs.[tool name]
and it seems to be the active tool, though the click events aren't registering.  And the UID that the local variable watch displays is not anywhere else in my code.  Perhaps it's making it up on the fly and not connecting to my code for what the tool is supposed to do.  And, once that tool is activated, I can't change tools to anything else.  For example I'll click on the Zoom Out Tool button and nothing happens.
0 Kudos
KevinAndras
Emerging Contributor
I've figured out how to do this:

Project > New Item > ArcGIS > Desktop Add-ins > Add-in Component > Tool

In the code module for the tool, you can add things like

Protected Overrides Sub OnMouseUp(ByVal arg As ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs)
   msgbox("Mouse up! You are at X = " & arg.X & " and Y = " & arg.Y)
End Sub

And to call the tool, you do this:

        Dim uid As UID = New ESRI.ArcGIS.esriSystem.UIDClass
        uid.Value = My.ThisAddIn.IDs.[toolname]
        Dim commandItem As ESRI.ArcGIS.Framework.ICommandItem = TryCast(document.CommandBars.Find(uid), ESRI.ArcGIS.Framework.ICommandItem)
        My.ArcMap.Application.CurrentTool = commandItem
0 Kudos
DaleHarris
Occasional Contributor
Thank K_Andras works a charm!!
0 Kudos