Customization With VB 2010
Customization With VB 2010
Customizing the ArcGIS user interface Customizing the ArcGIS user interface
An application or project may require unique workflows or map creation. The You can change the UI a little or a lot.
ArcGIS UI can be customized to support special tasks or products.
You can change or create toolbars, menus, and shortcut keys without
ArcGIS Desktop contains most of the functionality that you need but it has programming.
been designed to be general purpose. If a user or task does not require a
wide range of functions it can be customized to support just a few required
You can creating custom menus, buttons, and tools that you can
tasks.
program to perform custom tasks.
UI elements UI elements
Menus arrange other commands into a list. A context menu is a floating menu that
Standard ArcMap Menus and toolbar pops up at the location of the pointer when you right‐click.
Buttons and menu commands run programming code when you click them.
Tools require interaction with the display before an action is performed—that is,
before their programming code is run. The Zoom In tool is a good example—you click
or drag a rectangle over a map before seeing its contents in more detail.
Toolbars
lb can containi diff
different types off commands—menus,
d b
buttons, tools,
l Combo boxes allow you to choose an option from a drop‐down list
list.
combo boxes, or edit boxes.
Text boxes or edit boxes allow you to type in text.
Each command has code associated with it, either core to the application or
something you create.
1
4/6/2010
Using the Customize dialog box Using the Customize dialog box
A graphic interface for customization.
The way to tailor an application is to use the Customize dialog box to
change menus and toolbars.
Modify user interface
Click Customize on the Tools menu or right‐click any open toolbar and choose – Turn toolbars on and off
Customize from the menu.
– Create new toolbars
– Create new controls
When you open the Customize dialog box, you can modify existing menus, – Add,, delete,, and move
toolbars,
lb and
d context menus with
h simple
l drag‐and‐drop
d dd techniques.
h You can controls (buttons and
also create your own menus, toolbars, and context menus. menus)
– Manage shortcut keys
2
4/6/2010
A base template: A kind of map document that provides a quick way to create a
new map document. Templates may contain any of the following: data, a custom
interface,, and a predefined
p layout
y that arranges
g map p elements,, such as north
arrows, scale bars, and logos, on the virtual page. ArcMap templates have a .mxt file
extension. There may not always be a base template loaded in ArcMap.
Since any modifications made to the Normal template will be reflected every time A template provides specifications for a map document
you create or open a document, you should be careful when making changes to
Project templates can be created anywhere on your file
Normal.
system. Changes made to the project templates
propagate to all documents based on that template.
3
4/6/2010
Click the File menu and click Save As. • User Interface (UI) Controls
– UIButtons, UItools,
Click the Save as type drop‐down arrow and click ArcMap Templates (*.mxt). UIEditBoxs, and
UIComboBoxes
Navigate to the folder where you want the template saved. The template can be saved in • Control Properties
any folder on your network.
– Text or no text
Type a new name for the new template. You can't use Normal.mxt as a template name; – Image
that name is reserved for the Normal template. – Caption
– Group
Click Save
Creating custom commands with VBA Creating custom commands with VBA.
Click Create to create the control without attaching code to it. The name of the control
Visual Basic Applications is embedded in the applications and provides an
appears in the Commands list. You can add code for the control at another time. If you
integrated programming environment, the Visual Basic Editor (VBE). You can write
want to start adding code to the control right away, click Create and Edit.
Visual Basic (VB) macros, then debug and test them in the application e.g. ArcMap.
In ArcMap, each currently loaded document and template has an associated VBA The Save in: combo box
project in the VBE, allowing you to write macros that are specific to a given map lists the names of the
document or to apply macros to all the documents consuming a specific currently loaded
template. document and
templates In ArcMap
templates. ArcMap,
use this setting to
The VBA project for the document is called Project followed by the name of the
specify whether the
document in brackets. For example, in ArcMap it is named Project (<name of
changes you are about
document>.mxd), and the VBA project for the Normal template is called Normal
to make will be saved in
(Normal.mxt).
the document, the
Normal template, or
another template. The
default setting is the
Normal template.
4
4/6/2010
The ArcCatalog Normal template, Normal.gxt, is located in user settings. For example, in
Windows 2000 or XP, it can be found in C:\Documents and Settings\User\Application
Data\ESRI\ArcCatalog.
The ArcMap Normal template, Normal.mxt, is also located in user settings. For example, in
Windows 2000 or XP, it can be found in C:\Documents and Settings\User\Application
Data\ESRI\ArcMap\Templates.
5
4/6/2010
The Visual Basic Development Environment Adding modules and class modules
In the VBA development environment you can add modules, class modules, and
user forms to the default project contained in every ArcGIS application document. Once you've invoked the Visual Basic Editor, you can insert a module, class
module, or user form.
A project can consist of as many modules, class modules, and user forms as your
Then you insert a procedure or enter code for an existing event procedure in
work requires.
the item's Code window, where you can write, display, and edit code.
A user form is a container for user interface controls, such as command buttons and
text boxes.
To add your macro to a specific module, type the module name before the macro's Variables in your procedures may either be local or global. Global variables exist
name, for example, "Department.WorkMacro". If the module doesn't exist, a new during the entire time the code executes.
module with that name is created for you and added to the VBA project. Similarly,
if you provide a name for a new macro but don't specify which module to store it Local variables exist only while the procedure in which they are declared is running.
in, a new module, NewMacros, is created. The next time you execute a procedure, all local variables are reinitialized. You can
preserve the value of all local variables in a procedure for the code's lifetime by
declaring them static – fixes their value.
6
4/6/2010
The stub for a Sub procedure for the macro appears in the Code window. Click the Insert menu and click Procedure.
Type the code for the macro between the first line (Sub name()) and last line (End Type the name of the procedure in the Name text
Sub). box.
Declare variables before use to prevent mistakes. ' Get the ITable from the geodatabase
Dim pFact As IWorkspaceFactory
Use Public and Private to declare variables at module scope and Dim in local scope. Dim pWorkspace As IWorkspace
(Dim and Private mean the same at Module scope; however, using Private is more Dim pFeatws As IFeatureWorkspace
informative.) Dim pTable As ITable
7
4/6/2010
Running a macro in the Macros dialog box There is an extensive ArcObjects object library available to help you customize
Click the Tools menu, point to Macros, then click Macros. applications in ArcGIS Desktop. The ESRI Developer Network (EDN) describes the
classes, interfaces, properties, methods, and enumerations that are available
Click the Macros in drop‐down arrow and click the document or template containing
the macro you want to run.
If you are using VBA inside ArcMap or ArcCatalog, most of the common ESRI object
Type the name of the macro you want to run or click its name in the list. libraries are already referenced for you.
Click Run.
Working with Objects For interface references, declare an interface variable and use the Set statement
Your code manipulates objects by getting and setting properties on their to assign the interface reference to the property.
interfaces, such as setting the MaximumScale and MinimumScale of a map's
FeatureLayer; invoking methods on the interfaces, such as adding a vertex to a For other values, declare a variable with an explicit data type or use Visual
polyline; or setting a field's value. The code runs when an event occurs. Basic’s Variant data type. Then, use a simple assignment statement to assign the
value to the variable.
Properties
that are set by value do not require the Set statement.
8
4/6/2010
To run your code in the Visual Basic Editor or from the Macros dialog box
Private Function UIButtonControl1_ToolTip() As String Click the Tools menu and click Macros.
UIButtonControl1_ToolTip = "Selection Count" In the Macro list, click the macro you want and click Run.
End Function If the macro you want is not listed, make sure you've chosen the appropriate item:
either Normal, Project, or TemplateProject in the Macros In box. Private procedures
do not appear in any menus or dialog boxes.
9
4/6/2010
A control is a Visual Basic object you place on a user form that has its own
properties, methods, and events. You use controls to receive user input, display
output, and trigger event procedures. You can set the form to be either modal,
in which case the user must respond before using any other part of the
application, or modeless, in which case subsequent code is executed as it's
encountered.
VBA inserts a user form into your project and opens the Controls Toolbox. Click View, point to Toolbars, and check to show the toolbar with the command you want
to reset.
Click the controls in the Controls Toolbox that you want
to add to the form. Click the Tools menu and click Customize.
Add code to the user form or to its controls.
On the toolbar, right‐click the command you want to change.
To display the Code window for a user form or control,
double‐click the user form or control. Then, choose the Click Reset.
event you want your code to trigger from the The command returns to its default settings.
dropdown list of events and procedures in the Code
window and start typing your code. Or insert a
procedure and start typing code.
10
4/6/2010
Resources
Getting to Know ArcObjects: Programming ArcGIS with VBA
An introduction to VB and to ArcObjects
Focus on GUI, vector programming tasks. Doesn't get into much detail
on complex analytical solutions (i.e. spatial analysis)
ArcScripts
11