Button add-in problem (VB)

1897
7
Jump to solution
06-15-2012 08:07 AM
DaveCouture
New Contributor III
I just had VS2010 installed with ArcGIS 10.  I???m trying to learn how the snippet works and I'm using the AddLayerToActiveView snippet, as a test subject. I seem to have problems invoking it, in a button.  What am I doing wrong?

Imports ESRI.ArcGIS.SystemUI Imports ESRI.ArcGIS.esriSystem Imports ESRI.ArcGIS.Geodatabase Imports ESRI.ArcGIS.Catalog Imports ESRI.ArcGIS.Carto Namespace CustomUIElements     Public Class AddLayer         Inherits ESRI.ArcGIS.Desktop.AddIns.Button          Public Sub New()          End Sub          Protected Overrides Sub OnClick()              WHAT SHOULD I PUT HERE????                                     My.ArcMap.Application.CurrentTool = Nothing         End Sub          Protected Overrides Sub OnUpdate()             Enabled = My.ArcMap.Application IsNot Nothing         End Sub           Public Sub AddLayerToActiveView(ByVal activeView As IActiveView, ByVal layerPathFile As System.String)              If activeView Is Nothing OrElse layerPathFile Is Nothing OrElse (Not layerPathFile.EndsWith(".lyr")) Then                  Return              End If              ' Create a new GxLayer             Dim gxLayer As IGxLayer = New GxLayerClass              Dim gxFile As IGxFile = CType(gxLayer, IGxFile) 'Explicit Cast              ' Set the path for where the layerfile is located on disk             gxFile.Path = layerPathFile              ' Test if we have a valid layer and add it to the map             If Not (gxLayer.Layer Is Nothing) Then                  Dim map As IMap = activeView.FocusMap                 map.AddLayer(gxLayer.Layer)              End If          End Sub      End Class  End Namespace
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor
After you add the layer to the map, you'll have to refresh the active view.

My.ArcMap.Document.ActiveView.Refresh()

The button is getting disabled because My.ArcMap.Application is being evaluated as nothing. Why? I'm not sure. This probably leads to the question of why the button isn't disabled when you start the application. With Add-ins, you have to set the onDemand tag in the config.esriaddinx to False to make the Enabled state of the button work upon startup. See here for more information about delayed loading.

View solution in original post

0 Kudos
7 Replies
LeoDonahue
Occasional Contributor III
>> WHAT SHOULD I PUT HERE????

This:

AddLayerToActiveView(ByVal activeView As IActiveView, ByVal layerPathFile As System.String)
0 Kudos
DaveCouture
New Contributor III
Thanks for the reply, Leo, but I have already tried that and I get the following error: error BC30201: Expression expected

I think my problem is getting the active view to work, in the first argument.  I'm not sure what to put there, but I know the second argument should be the path of the layer file: AddLayerToActiveView(????, "D:\Contours.lyr")
0 Kudos
KenBuja
MVP Esteemed Contributor
You can use

AddLayerToActiveView(My.ArcMap.Document.ActiveView, "D:\Contours.lyr")
0 Kudos
DaveCouture
New Contributor III
Thanks Ken, that took care of the errors.  However, when I click the button, nothing happen and the button turns grey (disabled), after clicking on it.  Any thoughts?
0 Kudos
KenBuja
MVP Esteemed Contributor
After you add the layer to the map, you'll have to refresh the active view.

My.ArcMap.Document.ActiveView.Refresh()

The button is getting disabled because My.ArcMap.Application is being evaluated as nothing. Why? I'm not sure. This probably leads to the question of why the button isn't disabled when you start the application. With Add-ins, you have to set the onDemand tag in the config.esriaddinx to False to make the Enabled state of the button work upon startup. See here for more information about delayed loading.
0 Kudos
DaveCouture
New Contributor III
Thanks again, Ken, but it's still not working.  I'm not going to waste anymore time with this snippet.  I've tried other snippets and I didn't have any problems.  Maybe I'm missing a Reference, or something, to get this one to work.
0 Kudos