Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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, _
    "Normal.Module1.CreateNewFeatureClass"
pContextMenu.CreateMacroItem "Delete Layer", 13, _
    "Normal.Module1.DeleteFeatureClass"
pContextMenu.CreateMacroItem "Copy To...", 20, _
    "Normal.ThisDocument.CopyFeatures"
pContextMenu.CreateMacroItem "Draws Around...", 26, _
    "Normal.ThisDocument.DrawsAroundFeatures"
pContextMenu.CreateMacroItem "Clear Tool", 10, _
    "Normal.ThisDocument.ClearCurrentTool"
pContextMenu.CreateMacroItem "Delete Features", 13, _
    "Normal.Module1.DeleteFeatures"

' 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 
share|improve this question
    
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

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer
    
Thanks Kirk. Also found snippets for this (Create Context Menu). Works as you said. –  Jakub 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, _
                                      "Normal.Module1.CreateNewFeatureClass")
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 
share|improve this answer
    
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 Aug 26 '10 at 12:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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