Select to view content in your preferred language

VBA to VB .Net Select tool with context menu on mouse_down (right click)

634
2
08-24-2010 08:18 AM
JakubSisak
Honored Contributor
Hello all,
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, when a Mouse_Down receives Button = 2 parameter (right click) a popup a context menu with some items that call various other procedures.

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

I used the .NET sample 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.

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 "Sketch Tool", 27, "Normal.ThisDocument.SetSketchTool"
          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
0 Kudos
2 Replies
NeilClemmons
Honored Contributor
Create the context menu by implementing IMenuDef.  Call the context menu in your tool's OnContextMenu method:

    Public Function OnContextMenu(ByVal x As Integer, ByVal y As Integer) As Boolean Implements ESRI.ArcGIS.SystemUI.ITool.OnContextMenu
        ' Get the tool's context menu and show it.
        Dim uid As New UID
        uid.Value = "{" & ContextMenu.ClassId & "}"
        Dim menu As ICommandBar = DirectCast(m_application.Document.CommandBars.Find(uid), ICommandBar)
        menu.Popup()

        ' Return True so that ArcMap's Data Frame context menu is suppressed.
        Return True
    End Function
0 Kudos
JakubSisak
Honored Contributor
Thanks Neil,
Being very new to VB .Net i get the following problems:

1. Interface 'ESRI.ArcGIS.SystemUI.ITool' is not implemented by this class.

2. 'ClassId' is not a member of 'System.Windows.Forms.ContextMenu'
0 Kudos