About opening a geoprocessing tool's dialog box in .NET
The best approach to run a system tool—or any tool or model created and saved in a toolbox from a button—is to create an add-in control. Add-in controls were introduced at ArcGIS 10. You can create an add-in control through the Customize dialog box by selecting the Commands tab category, then Add-in Controls. For more information on getting started with add-ins, see Building add-ins for ArcGIS Desktop.
Once created, you can add the control to toolbars or menus. When you click a button (for example, in ArcMap), its OnClick method is called.
To start a geoprocessing tool from the button, copy and paste the following code example inside the code block of the OnClick method. The InvokeModal method of the IGPToolCommandHelper2 interface is called to open the tool. Remember to replace the name of the toolbox, and the name of the tool or script you want to reference. Pay close attention to the tool's actual location and name, not its label. You can get this information by right-clicking the tool in Catalog and reviewing the properties. For more information, see Toolbox properties: name, label, and alias in the ArcGIS Desktop Help system.
When you add the control, you can click the button and the Buffer tool appears. See the following code example:
[C#] protected override void OnClick()
{
//Set a reference to the IGPCommandHelper2 interface.
IGPToolCommandHelper2 pToolHelper=new GPToolCommandHelperClass()as
IGPToolCommandHelper2;
//Set the tool you want to invoke.
string toolboxName=@
"<ArcGIS install directory>\ArcToolbox\Toolboxes\Analysis Tools.tbx";
pToolHelper.SetToolByName(toolboxName, "Buffer");
//Create the messages object and a bool to pass to the InvokeModal method.
IGPMessages msgs;
msgs=new GPMessagesClass();
bool pok=true;
//Invoke the tool.
pToolHelper.InvokeModal(0, null, out pok, out msgs);
}
[VB.NET] Protected Overrides Sub OnClick()
'Set a reference to the IGPCommandHelper2 interface.
Dim pToolHelper As IGPToolCommandHelper2=New GPToolCommandHelper
'Set the tool you want to invoke.
Dim toolboxPath="<ArcGIS install directory>\ArcToolbox\Toolboxes\Analysis Tools.tbx"
pToolHelper.SetToolByName(toolboxPath, "Buffer")
'Create the messages object to pass to the InvokeModal method.
Dim msgs As IGPMessages
msgs=New GPMessages
'Invoke the tool.
pToolHelper.InvokeModal(0, Nothing, True, msgs)
My.ArcMap.Application.CurrentTool=Nothing
End Sub
To run a custom tool, set the catalog path of your custom toolbox and the name of the tool to the SetToolByName method. For example, to open MyTool of MyToolbox.tbx located at C:\temp, pass "C:\temp\MyToolbox.tbx", "MyTool" to the SetToolByName method.
See Also:
Building custom UI elements using add-insTo use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing | Deployment licensing |
---|---|
ArcGIS Desktop Basic | ArcGIS Desktop Basic |
ArcGIS Desktop Standard | ArcGIS Desktop Standard |
ArcGIS Desktop Advanced | ArcGIS Desktop Advanced |