English

How To: Implement ICommand in Visual Basic

Summary

To create a custom command in a DLL, an implementation code needs to be written. The instructions provided describe the implementation of ICommand.

Procedure

  1. Create a Visual Basic ActiveX DLL project.

    A. Start Visual Basic.
    B. Select a new ActiveX DLL project.
    C. From the Project Menu, click References.
    D. Navigate to the ESRI Object Library and check it on.
    E. In Project Explorer, select the Class.
    F. In the Properties Window, enter a meaningful name for the Class.
    G. In Project Explorer, select the Project.
    H. In the Properties Window, enter a meaningful name for the Project.
    I. From the Project Menu, click Properties and enter a meaningful Project Description.
    J. Save the project.


    As the Visual Basic ActiveX DLL project is created, include all of the following:

    * Name the Class 'clsMyCommand'
    * Name the Project 'AoDemo'
    * Enter 'ArcObjects MyCommand' for the Project Description

    * Save the class as clsMyCommand.cls
    * Save the VB project as AoDemo_clsMyCommand.vbp

    Note:
    For more information, refer to ArcObjects Online through the link, below, and navigate to Samples > How to use the samples.

  2. In the General Declarations section, enter:

    Code:
    Implements ICommand

    Press Enter.
  3. In the code window, use the Object (left hand) dropdown list to select ICommand.
  4. In the code window, use the Procedure (right hand) dropdown list to select the members of ICommand. The code window will be populated with wrapper lines of code for each property and method of ICommand.

    Option Explicit
    Implements ICommand

    Private Property Get ICommand_Bitmap() As esriCore.OLE_HANDLE

    End Property

    Private Property Get ICommand_Caption() As String

    End Property

    Private Property Get ICommand_Category() As String

    End Property

    Private Property Get ICommand_Checked() As Boolean

    End Property

    Private Property Get ICommand_Enabled() As Boolean

    End Property

    Private Property Get ICommand_HelpContextID() As Long

    End Property

    Private Property Get ICommand_HelpFile() As String

    End Property

    Private Property Get ICommand_Message() As String

    End Property

    Private Property Get ICommand_Name() As String

    End Property

    Private Sub ICommand_OnClick()

    End Sub

    Private Sub ICommand_OnCreate(ByVal hook As Object)

    End Sub

    Private Property Get ICommand_Tooltip() As String

    End Property


  5. Write implementation code for the Caption property.

    Code:
    Private Property Get ICommand_Caption() As String
    ICommand_Caption = "Caption: MyCommand"
    End Property


    Note:
    The word 'Caption' is included in the string for illustration purposes only. You will see the Caption for your custom command in ArcMap's Customize Dialog.

  6. Write implementation code for the Category property.

    Code:
    Private Property Get ICommand_Category() As String
    ICommand_Category = "ArcObjects Custom Commands"
    End Property

  7. Write implementation code for the Checked property.

    Code:
    Private Property Get ICommand_Checked() As Boolean
    ICommand_Checked = False
    End Property


    Note:
    False will leave the button in an unpressed state.

  8. Write implementation code for the Enabled property.

    Code:
    Private Property Get ICommand_Enabled() As Boolean
    ICommand_Enabled = True
    End Property


    Note:
    When the Enabled property is set to True, the user is able to click the button.

  9. Write the implementation code for the HelpContextID property.

    Code:
    Private Property Get ICommand_HelpContextID() As Long
    ICommand_HelpContextID = 0
    End Property


    Note:
    Use a value of 0 when there is no custom help.

  10. Write the implementation code for the HelpFile property.

    Code:
    Private Property Get ICommand_HelpFile() As Long
    ICommand_HelpFile = ""
    End Property


    Note:
    Use a zero-length string when there is no custom help.

  11. Write implementation code for the Message property.

    Code:
    Private Property Get ICommand_Message() As String
    ICommand_Message = "Message: MyCommand"
    End Property


    Note:
    The word 'Message' is included in the string for illustration purposes only. You will see the Message for your custom command on ArcMap's Status Bar when you hover the mouse over your command.

  12. Write implementation code for the Name property.

    Code:
    Private Property Get ICommand_Name() As String
    ICommand_Name = "MyCommand"
    End Property

  13. Write implementation code for the Tooltip property.

    Code:
    Private Property Get ICommand_Tooltip() As String
    ICommand_Tooltip = "Tooltip: MyCommand"
    End Property


    Note:
    The word 'Tooltip' is included in the string for illustration purposes only. You will see the Tooltip when the mouse is hovered over your command.

  14. In this step and the next, write the implementation code that associates your command with ArcMap:
    In the General Declarations section, add a module-level object variable to hold a reference to the application object.

    Code:
    Option Explicit

    Private m_pApp As IApplication
    Implements ICommand

  15. In the OnCreate method, pass the hook to the application object.

    Code:
    Private Sub ICommand_OnCreate(ByVal hook As Object)
    Set m_pApp = hook
    End Sub

  16. Write the code that will execute when the button is clicked.

    Code:
    Private Sub ICommand_OnClick()
    MsgBox "MyCommand"
    m_pApp.Caption = "The OnClick method for MyCommand has executed"
    End Sub

  17. In the code window, use the Object dropdown list to select Class. In the code window, use the Procedure dropdown list to select each of the members of Class.
  18. In Class Terminate, release the application reference.

    Code:
    Private Sub Class_Terminate()
    Set m_pApp = Nothing
    End Sub

  19. Your implementation code is ready to compile.

    Option Explicit

    Private m_pApp As IApplication
    Implements ICommand

    Private Sub Class_Initialize()

    End Sub

    Private Sub Class_Terminate()
    Set m_pApp = Nothing
    End Sub

    Private Property Get ICommand_Bitmap() As esriCore.OLE_HANDLE

    End Property

    Private Property Get ICommand_Caption() As String
    ICommand_Caption = "Caption: MyCommand"
    End Property

    Private Property Get ICommand_Category() As String
    ICommand_Category = "ArcObjects Custom Commands"
    End Property

    Private Property Get ICommand_Checked() As Boolean
    ICommand_Checked = False
    End Property

    Private Property Get ICommand_Enabled() As Boolean
    ICommand_Enabled = True
    End Property

    Private Property Get ICommand_HelpContextID() As Long
    ICommand_HelpContextID = 0
    End Property

    Private Property Get ICommand_HelpFile() As String
    ICommand_HelpFile = ""
    End Property

    Private Property Get ICommand_Message() As String
    ICommand_Message = "Message: MyCommand"
    End Property

    Private Property Get ICommand_Name() As String
    ICommand_Name = "MyCommand"
    End Property

    Private Sub ICommand_OnClick()
    MsgBox "MyCommand"
    m_pApp.Caption = "The OnClick method for MyCommand has executed"
    End Sub

    Private Sub ICommand_OnCreate(ByVal hook As Object)
    Set m_pApp = hook
    End Sub

    Private Property Get ICommand_Tooltip() As String
    ICommand_Tooltip = "Tooltip: MyCommand"
    End Property



    Note:
    The implementation for the Bitmap property has intentionally been omitted.

  20. Save the project.
  21. Compile the DLL as AoDemo_clsMyCommand.dll.

    A. Click the File Menu and select Make DLL.
    B. From the Project Menu, select Properties.
    C. Select the Component Tab.
    D. Click Binary Compatibility.
    E. Click the File Menu and select Make DLL.

  22. Register your custom command with ArcMap.

    A. If it is not already running, start the ArcGIS application (ArcMap, ArcCatalog, or ArcScene) to which you want to add your custom command.
    B. Click the Tools menu and select Customize.
    C. Select the Commands Tab.
    D. In the lower left corner of the dialog, use the pulldown to select where to save your command.
    E. Click 'Add from file'.
    F. Navigate to the DLL that contains the custom command.
    G. Select the file and click Open.
    H. When the 'Added Objects' dialog box appears, it reports the new objects that have been registered with ArcMap, ArcCatalog, or ArcScene. Click OK.
    I. Click and drag the command from the Commands list and drop it onto a toolbar. Note that the Category you assigned appears in the left-hand panel.
    J. Click Close.


  23. Test the implementation.

Related Information