An introduction to ArcGIS add-ins
In this section, we will introduce the concept of the ArcGIS add-ins building approach. We will then build our first Hello, ArcGIS project, which will help us get started with ArcGIS add-ins. The Hello, ArcGIS project will be a small example that explains the basics of add-ins. As explained in the previous chapter, there are different approaches to building applications on ArcGIS for Desktop. The first and oldest method is Visual Basic for Applications (VBA), the second one is add-in, and the third one is extending ArcObjects, which we will be using in the last two examples.
The reason I started with add-ins is because it is the easiest and most convenient method for development and deployment. When you write and deploy an add-in, ArcGIS detects it and asks you whether you want to install it or not. Add-ins can be later disabled or enabled based on need, which makes them a secure approach for development.
Creating the Hello, ArcGIS add-in project
In this section, we will show how to create an add-in project using Visual Studio. To create an add-in project for ArcGIS, follow these steps:
- From the Start menu, locate and run Microsoft Visual Studio.
- From the Visual Studio application, point to the File menu and then click on New Project.
- Under the Templates node, expand Visual Basic, this is the language we will be programming with, then expand ArcGIS, and click on Desktop Add-ins. You can write add-ins for all ArcGIS for Desktop products as you can see. We want to write add-ins on top of ArcMap, so select ArcMap Add-ins.
- In the Name field, type the name of the project
HelloArcGIS
and point the location to theC:\ArcGISByExample\HelloArcGIS
folder, as illustrated in the following screenshot: - Click on OK, this will show the ArcGIS Add-in Wizard.
- We can use the wizard to autogenerate our controls and other codes, but we are going to do that manually, so simply click on Finish to create the project.
- Once the project has been created, you will see two files:
config.esriaddinx
and an image as an icon for your add-in. Theconfig.esriaddinx
file contains metadata about your project like the name and description. To add a functionality to our project, we need to add a control to do this, point to the Project menu and then click on Add Class. - From the Common Items node expand the ArcGIS node and then click on Desktop Add-ins. Click on Add-in Component and call it
btnHelloArcGIS
, and then click on Add. - From the Add-in Wizard, select Button, for the button caption type
Hello ArcGIS
and then click on Finish. - This adds two files: a class and an image. Double-click on the
btnHelloArcGIS
class. - Note that this class inherits from a base class
ESRI.ArcGIS.Desktop.AddIns.Button
, which will give it the functionality of a button. What is interesting for us here is theonClick
event, which is the code that will get executed when one clicks on the button. Write the following code in theonClick
event:MsgBox("Hello, ArcGIS!")
For more details, take a look at the following screenshot:
- It is time to build our project and see the fruits of our work. Point to the Build menu and then click on Build solution. You should get a Build succeeded status message if you have written the code correctly.
- Run ArcMap.
- From the Customize menu, click on Customize Mode.
- Activate the Commands tab and then type
Hello ArcGIS
in the search box, as illustrated in the following screenshot. Drag the Hello ArcGIS button near the Zoom in button to add it to the toolbar. - Close the Customize dialog.
- By clicking on your new button, you will notice that the message Hello ArcGIS is displayed.
- To add your own toolbar, add a new class to your project and choose Add-in Command Container.
- Select Toolbar from the wizard and then select your button to be added to the toolbar from the drop-down list.
- Close ArcMap and then close Visual Studio.