This should be simple... I am converting VBA customization to VB .NET in VS 2008 Express Edition. In VBA i have a custom selection tool. Aside from just selecting features, when OnMouseDown receives Button = 2 parameter (right click) I popup a context menu with some macro items that call various other procedures.

I need some help with making this work in .Net...

I used the ESRI .NET sample project to get the feel for this and i am sure i can re-create the select tool but i am having problems with the context menu. I would also like to know how to call procedures when user clicks the context menu item.

Private Sub UIToolControl1_MouseDown(ByVal button As Long, _
    ByVal shift As Long, ByVal x As Long, ByVal y As Long)

Dim pLayer As ILayer
Dim pContextMenu As ICommandBar
Dim pAppPosition As IWindowPosition
Dim pPoint As POINTAPI

If pMxDoc.SelectedLayer Is Nothing Then
MsgBox "Select a layer in the TOC!": GoTo ep
End If

Select Case button
Case 1
Set pLayer = pMxDoc.SelectedLayer
If pLayer Is Nothing Then
MsgBox "You must select 1 feature layer in the TOC!": GoTo ep
End If

If Not TypeOf pLayer Is IGeoFeatureLayer Then
MsgBox "You must select 1 feature layer in the TOC!": GoTo ep
End If

Set m_pPoint = _
    pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y)
m_bIsMouseDown = True
Case 2
'on right click = popup
Set pContextMenu = CommandBars.Create("Go2Grid", esriCmdBarTypeShortcutMenu)

Set pAppPosition = Application

' Add 3 built in commands to the new context menu using the
' built-in ArcID module.

pContextMenu.CreateMacroItem "New Feature Class", 8, _
pContextMenu.CreateMacroItem "Delete Layer", 13, _
pContextMenu.CreateMacroItem "Copy To...", 20, _
pContextMenu.CreateMacroItem "Draws Around...", 26, _
pContextMenu.CreateMacroItem "Clear Tool", 10, _
pContextMenu.CreateMacroItem "Delete Features", 13, _

' Popup the menu.

pPoint.x = x
pPoint.y = y
ClientToScreen pActiveView.ScreenDisplay.hwnd, pPoint

pContextMenu.Popup pPoint.x, pPoint.y
Case Else
GoTo ep
End Select

End Sub 
  • Jakub, Would you be able to highlight you code and hit the code format button in the edit window. Will make it a bit easier to read. – Nathan W Aug 26 '10 at 2:46

Instead of calling ICommandBar.CreateMacroItem, I'd create a BaseMenu class at design time and populate it with Command progIDs. Each VBA macro will need to be converted into a command - I'd inherit BaseCommand for those. If you really do need dynamic menu's, have one of the commands implement IMultiItem.

Your ITool would use ICommandBars.Find to find the menu and call Popup on it.

I recommend putting all code within your mousedown handler within a try/catch block.

  • Thanks Kirk. Also found snippets for this (Create Context Menu). Works as you said. – Jakub Sisak GeoGraphics Oct 27 '10 at 17:46

I have never used ArcObjects before but just by looking at the docs there is a OnClick event on the Command for each ICommandItem that pContextMenu.CreateMacroItem will return.

So I guess something like this would work:

Dim commandItem as ICommandItem 
commandItem = pContextMenu.CreateMacroItem(_
                                      "New Feature Class", _
                                      8, _
Dim command as ICommand 
command = commandItem.Command
AddHandler command.OnClick, AddressOf Command_OnClick

Private Sub Command_OnClick()
  'In this example, a message box is displayed.
   MsgBox "Clicked on my command"
End Sub 
  • Thanks. I will try this once i figure out the first part of my problem which is the Select Tool itself. I'd like to be able to select features on the map just as i have done with the "VBA" tool but at the same time when the OnMouseDown event fires and receives the Button = 2 parameter (Right click instead of left) i would like to popup the context menu instead. – Jakub Sisak GeoGraphics Aug 26 '10 at 12:41

you have to control it by OnContextMenu event. you have to add an extension into your project.

enter image description here

then in context menu event you can control that if your tool is active, context menu popup else don't popup.

 Private Sub OnContextMenu(x As Integer, y As Integer, ByRef handled As Boolean)
    If YourCommandActiveCondition Then

        Dim pContextMenu As ICommandBar
        Dim commandBars As ESRI.ArcGIS.Framework.ICommandBars = m_application.Document.CommandBars
        pContextMenu = commandBars.Create("MyContextMenu", _

        ' Add 3 built in commands to the new context menu using the built in 
        ' ArcID module.
        Dim optionalIndex As System.Object = System.Type.Missing
        Dim uid As ESRI.ArcGIS.esriSystem.UID = New ESRI.ArcGIS.esriSystem.UIDClass()

        uid.Value = "esriArcMapUI.ZoomInFixedCommand"
        ' Can use CLSID or ProgID
        uid.SubType = 0
        pContextMenu.Add(uid, optionalIndex)

        uid.Value = "{119591DB-0255-11D2-8D20-080009EE4E51}"
        ' Can use CLSID or ProgID
        uid.SubType = 1
        pContextMenu.Add(uid, optionalIndex)

        uid.Value = "{119591DB-0255-11D2-8D20-080009EE4E51}" ' export map
        ' Can use CLSID or ProgID
        uid.SubType = 2
        pContextMenu.Add(uid, optionalIndex)
        ' Popup the menu.
        handled = True
        ' Let the application know that the OnContextMenu event was handled.
    End If
End Sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.