{"id":25,"date":"2010-12-06T14:17:20","date_gmt":"2010-12-06T22:17:20","guid":{"rendered":"http:\/\/gis.qtools.com\/blog\/"},"modified":"2016-04-11T12:09:03","modified_gmt":"2016-04-11T19:09:03","slug":"part-4-creating-new-add-in-project","status":"publish","type":"page","link":"https:\/\/gis.qtools.com\/blog\/tutorials\/vba-to-c-add-in\/part-4-creating-new-add-in-project\/","title":{"rendered":"Part 4: Creating New Add-in Project"},"content":{"rendered":"<p><strong>Tutorial Navigation<\/strong> | Previous: <a href=\"..\/part-3-setting-up-visual-studio\/\">Part 3: Setting Up Visual Studio<\/a> | Next: <a href=\"..\/part-5-adding-utility-dialog-form\/\">Part 5: Adding the Utility&#8217;s Dialog Form<\/a><\/p>\n<p>In this step, we will use the ArcMap Add-in template wizard in Visual Studio to create a project that implements a button on a toolbar.<\/p>\n<h1>Resources<\/h1>\n<h3>Files<\/h3>\n<p>The files needed for this part of the tutuorial can be downloaded here: <a href=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/data\/VBA_to_CS_Data.zip\">Tutorial Data<\/a> (96 KB)<br \/>\nContents (click to expand for more information):<\/p>\n<ul>\n<li>VBAtoCS_Tutorial_01_v10.mxd (<a href=\"#\" onclick=\"xcollapse('X7146');return false;\">+\/-<\/a>)\n<div id=\"X7146\" style=\"display: none; background: transparent;\">\nSample map document to use when testing and debugging the C# Add-in.<\/p>\n<\/div>\n<\/li>\n<li>sample_data.mdb (<a href=\"#\" onclick=\"xcollapse('X3412');return false;\">+\/-<\/a>)\n<div id=\"X3412\" style=\"display: none; background: transparent;\">\nPersonal geodatabase with one feature class, &#8220;Hwy_94_Overlaps&#8221;, containing 13 line features of various lengths that overlap on the same highway. <\/p>\n<\/div>\n<\/li>\n<li>StaggerOffsetFeatures_Addin_values.TXT (<a href=\"#\" onclick=\"xcollapse('X10509');return false;\">+\/-<\/a>)\n<div id=\"X10509\" style=\"display: none; background: transparent;\">\nSimple text file with text that can be copied and pasted into wizard and code files in Visual Studio C#. <\/p>\n<\/div>\n<\/li>\n<li>StaggerOffsetFeatures.png (<a href=\"#\" onclick=\"xcollapse('X3131');return false;\">+\/-<\/a>)\n<div id=\"X3131\" style=\"display: none; background: transparent;\">\nBitmap to use for the Add-In&#8217;s UI button.<\/p>\n<\/div>\n<\/li>\n<li>VBA_OffsetFeatures.txt (<a href=\"#\" onclick=\"xcollapse('X9405');return false;\">+\/-<\/a>)\n<div id=\"X9405\" style=\"display: none; background: transparent;\">\nVBA source code for the utility that can be copy and pasted into the C# code editor to use for reference. <\/p>\n<\/div>\n<\/li>\n<li>VBA_OffsetFeatures_Form.png (<a href=\"#\" onclick=\"xcollapse('X3667');return false;\">+\/-<\/a>)\n<div id=\"X3667\" style=\"display: none; background: transparent;\">\nImage of the VBA utility&#8217;s dialog form, to use as a reference when re-creating the form in C#.<\/p>\n<\/div>\n<\/li>\n<\/ul>\n<h3>Video Demonstrations<\/h3>\n<p>The following videos are also linked from the relevant sections below. (opens in a new browser window or tab)<\/p>\n<p><a href=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/video\/CS2008_NewAddin_Demo_pt1.swf.html\" target=\"_blank\">New C# Project dialog<\/a> (duration 0:41)<br \/>\n<a href=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/video\/CS2008_NewAddin_Demo_pt2.swf.html\" target=\"_blank\">ArcGIS Add-in Wizard, Welcome page<\/a> (duration 0:26)<br \/>\n<a href=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/video\/CS2008_NewAddin_Demo_pt3.swf.html\" target=\"_blank\">ArcGIS Add-in Wizard, Button page<\/a> (duration 0:57)<br \/>\n<a href=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/video\/CS2008_NewAddin_Demo_pt4.swf.html\" target=\"_blank\">Add a toolbar<\/a> (duration 0:46)<br \/>\n<a href=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/video\/CS2008_NewAddin_Demo_pt5.swf.html\" target=\"_blank\">Add simple test code<\/a> (duration 0:47)<br \/>\n<a href=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/video\/CS2008_NewAddin_Demo_pt6.swf.html\" target=\"_blank\">Test the Add-in in ArcMap<\/a> (duration 0:46)<\/p>\n<h1>Create the New Add-in Project<\/h1>\n<p>Exit ArcMap if it is running.<\/p>\n<p>Launch Visual Studio.<\/p>\n<h2>Start a New C# Project<\/h2>\n<p><b>Video Demonstration:<\/b> <a href=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/video\/CS2008_NewAddin_Demo_pt1.swf.html\" target=\"_blank\">New C# Project dialog<\/a> (duration 0:41)<\/p>\n<h3>Initiate an Add-in Project with a Button<\/h3>\n<p>Select <em>New Project&#8230; <\/em>from the<em> File <\/em>menu. Expand the list on the left of the <em>New Project <\/em>dialog and select the <em>Desktop Add-Ins<\/em> item. Select the <em>ArcMap Add-in <\/em>template. Enter a <em>Name <\/em>for the project, such as &#8220;VBA_to_CS_Tutorial&#8221;. Set the folder <em>Location<\/em> where you want your source code stored. Enter a <em>Solution Name<\/em>, such as &#8220;VBA_to_CS_Tut_Sol&#8221;. Click the <em>OK <\/em>button.<\/p>\n<p><img decoding=\"async\" src=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/images\/CS2008_NewAddin_01.png\" \/><\/p>\n<p>After a moment, the <em>ArcGIS Add-Ins Wizard <\/em>displays.<\/p>\n<h2>ArcGIS Add-In Wizard Welcome Page<\/h2>\n<p><b>Video Demonstration:<\/b> <a href=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/video\/CS2008_NewAddin_Demo_pt2.swf.html\" target=\"_blank\">ArcGIS Add-in Wizard, Welcome page<\/a> (duration 0:26)<\/p>\n<p>The Welcome page of the wizard provides for editing the basic information about the Add-in that will be displayed by the Add-in Manager in ArcGIS. You may edit values to your choice. Click the <em>Next <\/em>button.<\/p>\n<p><img decoding=\"async\" src=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/images\/CS2008_NewAddin_02.png\" \/><\/p>\n<h2>ArcGIS Add-In Wizard Button Page<\/h2>\n<p><b>Video Demonstration:<\/b> <a href=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/video\/CS2008_NewAddin_Demo_pt3.swf.html\" target=\"_blank\">ArcGIS Add-in Wizard, Button page<\/a> (duration 0:57)<\/p>\n<p>Check the box next <em>Button<\/em> on the left side of the dialog under <em>Add-in Types<\/em>. <\/p>\n<p><img decoding=\"async\" src=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/images\/CS2008_NewAddin_03.png\" \/><\/p>\n<p>Enter the following values:<br \/>\n<em>Class Name <\/em>: StaggerOffsetButton<br \/>\n<em>Caption<\/em>: Stagger Offset Features<br \/>\n<em>Image<\/em>: (click the browse button and import the provided <em>StaggerOffsetFeatures.png<\/em> file).<br \/>\n<em>Category<\/em>: accept default, &#8220;Add-in Controls&#8221;.<br \/>\n<em>On Demand<\/em>: Checked<br \/>\n<em>Tooltip<\/em>: Stagger Offset Features: Offsets all features in selected layer.<br \/>\n<em>Description<\/em>:  Stagger Offset: Offsets all feature in selected layer by a user-specified stagger amount on either side of the original position. Start an edit session before use.<\/p>\n<p>Click the <em>Finish <\/em>button. The project framework is built and will open in Visual Studio. Initially, only the <em>Config.esriaddinx <\/em>file will open and be displayed in the IDE.<\/p>\n<p>The <em>Config.esriaddinx <\/em>file is an XML format file used by ArcGIS to identify and configure Add-ins. It is managed by the wizards and you should not need to mess with it.<\/p>\n<h2>Add a Toolbar to the Project<\/h2>\n<p><b>Video Demonstration:<\/b> <a href=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/video\/CS2008_NewAddin_Demo_pt4.swf.html\" target=\"_blank\">Add a toolbar<\/a> (duration 0:46)<\/p>\n<p>Open the Solution Explorer if it is not visible (<em>View | Solution Explorer<\/em>). Right-click on the project name (&#8220;VBA_to_CS_Tutorial&#8221;) and select <em>Add | New Item&#8230;<\/em> from the context menu.<\/p>\n<p><img decoding=\"async\" src=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/images\/CS2008_NewAddin_04.png\" \/><\/p>\n<p>Locate and select the <em>Desktop Add-Ins<\/em> item in the <em>Categories <\/em>list. Under <em>Templates<\/em>, select <em>Add-in component<\/em>. Since we are adding a toolbar, you can ignore the value in the <em>Name <\/em>box, it will not be used. Click the <em>OK <\/em>button.<\/p>\n<p><img decoding=\"async\" src=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/images\/CS2008_NewAddin_05.png\" \/><\/p>\n<p>The <em>ArcGIS Add-ins Wizard <\/em>displays again, but this time displays only Add-in components.<\/p>\n<p>Click the <em>Add-in Command Bars <\/em>item on the left. Select the <em>Toolbar <\/em>item.<\/p>\n<p><img decoding=\"async\" src=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/images\/CS2008_NewAddin_06.png\" \/><\/p>\n<p>Enter the following values:<br \/>\n<em>Caption<\/em>: Tutorial_Toolbar<br \/>\n<em>Premier Toolbar<\/em>: checked (so that toolbar is displayed by default)<br \/>\n<em>Items<\/em>: click <em>Add Item<\/em> underneath <em>Reference ID <\/em>and select the StaggerOffsetsButton.<\/p>\n<p>Click the <em>Finish <\/em>button.<\/p>\n<p>Expand this to view contents of the <em>Config.esriaddinx <\/em>file that includes the button and toolbar (<a href=\"#\" onclick=\"xcollapse('X8762');return false;\">+\/-<\/a>)<\/p>\n<div id=\"X8762\" style=\"display: none; background: transparent;\">\nIn the <em>AddIn <\/em>section of this XML file you will find configuration information for the button and toolbar you just created.<\/p>\n<pre lang=\"xml\" line=\"1\">\r\n<ESRI.Configuration xmlns=\"http:\/\/schemas.esri.com\/Desktop\/AddIns\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\">\r\n  <Name>VBA_to_CS_Tutorial<\/Name>\r\n  <AddInID>{34b94edb-b71a-4fde-bcdf-c344da8774d1}<\/AddInID>\r\n  <Description>VBA_to_CS_Tutorial<\/Description>\r\n  <Version>1.0<\/Version>\r\n  <Image>Images\\VBA_to_CS_Tutorial.png<\/Image>\r\n  <Author>jeffh<\/Author>\r\n  <Company>Qtools<\/Company>\r\n  <Date>12\/7\/2010<\/Date>\r\n  <Targets>\r\n    <Target name=\"Desktop\" version=\"10.0\" \/>\r\n  <\/Targets>\r\n  <AddIn language=\"CLR\" library=\"VBA_to_CS_Tutorial.dll\" namespace=\"VBA_to_CS_Tutorial\">\r\n    <ArcMap>\r\n      <Commands>\r\n        <Button id=\"Qtools_VBA_to_CS_Tutorial_StaggerOffsetButton\" class=\"StaggerOffsetButton\" message=\"Stagger Offset: Offsets all feature in selected layer by a user-specified stagger amount on either side of the original position. Start an edit session before use.\" caption=\"Stagger Offset Features\" tip=\"Stagger Offset Features: Offsets all features in selected layer.\" category=\"Add-In Controls\" image=\"Images\\StaggerOffsetButton.png\" \/>\r\n      <\/Commands>\r\n      <Toolbars>\r\n        <Toolbar id=\"Qtools_VBA_to_CS_Tutorial_Tutorial_Toolbar\" caption=\"Tutorial_Toolbar\" showInitially=\"true\">\r\n          <Items>\r\n            <Button refID=\"Qtools_VBA_to_CS_Tutorial_StaggerOffsetButton\" \/>\r\n          <\/Items>\r\n        <\/Toolbar>\r\n      <\/Toolbars>\r\n    <\/ArcMap>\r\n  <\/AddIn>\r\n<\/ESRI.Configuration>\r\n<\/pre>\n<\/div>\n<h2>Add Code to Test the Add-In<\/h2>\n<p><b>Video Demonstration:<\/b> <a href=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/video\/CS2008_NewAddin_Demo_pt5.swf.html\" target=\"_blank\">Add simple test code<\/a> (duration 0:47)<\/p>\n<p>Before starting with the VBA conversion and coding, we will test that the project framework produces an Add-in that works with ArcMap.<\/p>\n<p>In the <em>Solution Explorer<\/em>, open the <em>StaggerOffsetButton.cs <\/em>file (double-click it). It will display in the Code Editor.<\/p>\n<p><img decoding=\"async\" src=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/images\/CS2008_NewAddin_07.png\" \/><\/p>\n<p>Expand this to see all the pre-filled code created by the wizard in the button class file (<a href=\"#\" onclick=\"xcollapse('X830');return false;\">+\/-<\/a>)<\/p>\n<div id=\"X830\" style=\"display: none; background: transparent;\">\n<pre lang=\"csharp\" line=\"1\">\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.Text;\r\nusing System.IO;\r\n\r\nnamespace VBA_to_CS_Tutorial\r\n{\r\n    public class StaggerOffsetButton : ESRI.ArcGIS.Desktop.AddIns.Button\r\n    {\r\n        public StaggerOffsetButton()\r\n        {\r\n        }\r\n\r\n        protected override void OnUpdate()\r\n        {\r\n            Enabled = ArcMap.Application != null;\r\n        }\r\n\r\n        protected override void OnClick()\r\n        {\r\n            \/\/\r\n            \/\/  TODO: Sample code showing how to access button host\r\n            \/\/\r\n            ArcMap.Application.CurrentTool = null;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<\/div>\n<p>Locate the method named <em>OnClick()<\/em>. The Add-in wizard pre-filled it with some sample code.<\/p>\n<pre lang=\"csharp\" line=\"19\">\r\nprotected override void OnClick()\r\n{\r\n    \/\/\r\n    \/\/  TODO: Sample code showing how to access button host\r\n    \/\/\r\n    ArcMap.Application.CurrentTool = null;\r\n}\r\n<\/pre>\n<p>This is the entry point for your code when the Add-in&#8217;s button is clicked. <\/p>\n<p>Our test will display a simple message box in ArcMap. The <em>MessageBox<\/em> class needs a reference to <em>System.Windows.Forms<\/em>. As you work with various objects and interfaces, you will need to add references to the assemblies that contain them, to your project.<\/p>\n<p>Right-click on <em>References <\/em>in the <em>Solution Explorer<\/em>, and select <em>Add Reference&#8230; <\/em>from the context menu.<br \/>\nOn the <em>.NET <\/em>tab page, locate and select <em>System.Windows.Forms<\/em>, click <em>OK<\/em>.<\/p>\n<p><img decoding=\"async\" src=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/images\/CS2008_NewAddin_08.png\" \/><\/p>\n<p>Once a reference to an assembly has been added to your project, you can &#8220;use&#8221; it in any file where you need it.<\/p>\n<p>In the code window, scroll to the top of the <em>StaggerOffsetButton.cs <\/em> file. Add another line under the last <code>using<\/code> statement:<br \/>\n<code>using System.Windows.Forms;<\/code><\/p>\n<pre lang=\"csharp\" line=\"1\">\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.Text;\r\nusing System.IO;\r\nusing System.Windows.Forms;\r\n<\/pre>\n<p>Now the MessageBox class is available to us.<\/p>\n<p>Insert the following code to display a simple dialog box in ArcMap:<br \/>\n<code>MessageBox.Show(\"It WORKS!!!\", \"VBA to CS Tutorial\");<\/code><\/p>\n<pre lang=\"csharp\" line=\"19\">\r\nprotected override void OnClick()\r\n{\r\n    \/\/\r\n    \/\/  TODO: Sample code showing how to access button host\r\n    \/\/\r\n    ArcMap.Application.CurrentTool = null;\r\n\r\n    MessageBox.Show(\"It WORKS!!!\", \"VBA to CS Tutorial\");\r\n}\r\n<\/pre>\n<h2>Run the Add-In in ArcMap Using Visual Studio Debugger<\/h2>\n<p><b>Video Demonstration:<\/b> <a href=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/video\/CS2008_NewAddin_Demo_pt6.swf.html\" target=\"_blank\">Test the Add-in in ArcMap<\/a> (duration 0:46)<\/p>\n<p>To compile the new Add-in, add it to the Add-ins folder, and launch ArcMap, either:<br \/>\nClick the little green right-pointing triangle button.<br \/>\n-or- Press <em>F5<\/em>.<br \/>\n-or- Select <em>Start Debugging <\/em>from the <em>Debug<\/em> menu.<\/p>\n<p><img decoding=\"async\" src=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/images\/CS2008_NewAddin_09.png\" \/><\/p>\n<p>Be patient for ArcMap to load. Once it opens, you should open the provided <em>VBAtoCS_Tutorial_01_v10.mxd <\/em>map. After the map opens fully, you will notice a small floating toolbar with a single button. Drag it to an empty space on a tool panel.<\/p>\n<p><img decoding=\"async\" src=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/images\/CS2008_NewAddin_10.png\" \/><\/p>\n<p>Hover over the button to see the tooltip. Click the button to display the &#8220;It WORKS!!!&#8221; dialog.<\/p>\n<p><img decoding=\"async\" src=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/images\/CS2008_NewAddin_11.png\" \/><\/p>\n<p>Select <em>Add-In Manager <\/em>from the <em>Customize <\/em>menu. Select the <em>VBA_to_CS_Tutorial<\/em> item on the left to view details about the Add-In.<\/p>\n<p><img decoding=\"async\" src=\"\/\/gis.qtools.com\/tutorials\/vba_to_cs\/images\/CS2008_NewAddin_12.png\" \/><\/p>\n<p>Close ArcMap to return to Visual Studio and stop debugging.<\/p>\n<p>Now that the framework for the Add-In is working, it&#8217;s simply a matter of reproducing the dialog form and coding with ArcObjects.<\/p>\n<p><strong>Tutorial Navigation<\/strong> | Previous: <a href=\"..\/part-3-setting-up-visual-studio\/\">Part 3: Setting Up Visual Studio<\/a> | Next: <a href=\"..\/part-5-adding-utility-dialog-form\/\">Part 5: Adding the Utility&#8217;s Dialog Form<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tutorial Navigation | Previous: Part 3: Setting Up Visual Studio | Next: Part 5: Adding the Utility&#8217;s Dialog Form In this step, we will use the ArcMap Add-in template wizard in Visual Studio to create a project that implements a button on a toolbar. Resources Files The files needed for this part of the tutuorial [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":8,"menu_order":4,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-25","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/gis.qtools.com\/blog\/wp-json\/wp\/v2\/pages\/25","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gis.qtools.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/gis.qtools.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/gis.qtools.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/gis.qtools.com\/blog\/wp-json\/wp\/v2\/comments?post=25"}],"version-history":[{"count":2,"href":"https:\/\/gis.qtools.com\/blog\/wp-json\/wp\/v2\/pages\/25\/revisions"}],"predecessor-version":[{"id":85,"href":"https:\/\/gis.qtools.com\/blog\/wp-json\/wp\/v2\/pages\/25\/revisions\/85"}],"up":[{"embeddable":true,"href":"https:\/\/gis.qtools.com\/blog\/wp-json\/wp\/v2\/pages\/8"}],"wp:attachment":[{"href":"https:\/\/gis.qtools.com\/blog\/wp-json\/wp\/v2\/media?parent=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}