This is the Personal Stock Monitor scripting developers forum.
Subscribe to RSS Feed
Developing Custom Stock Market Software using PSM -> Add an Item to a Context Menu?
Not logged in.
\n\n[/code]");
2011-01-30 14:02:30
1 of 5
#2900
Howdy Everyone,

I would like to add a menu item to the context menu that comes up when right-clicking on a selected ticker. The context menu is defined as "Main" via menu.ApplicationContext (just added to the new release).

What is the MenuManager property for a context menu? The only property for MenuManager that's listed on the documentation web page is "MainMenu". When using "Set ContextMenu = MenuManager.MainMenu", my menu item is added to the menu bar at the top of the window, to the right of "Help".

I tried using "Set ContextMenu = MenuManager.ContextMenu" but this results in an error.

I've included my current VBScript code below. Obviously, the code will be moved into the extension it is meant for, once it's working properly.

Thank you all for any help you can provide,

-Don


<pss_extension name="Context Menus" version="1.0">Context Menus
<script language="VBScript">
<![CDATA[

Class MyCMFormHandler


'-----------------------------------------------------------------------------
public Function OnMenuItemSelected ( id )
'-----------------------------------------------------------------------------
  If id = G_MyContextMenuID Then
     MsgBox "Code here to perform the function", vbOKOnly, "Message"
     Exit Function
  End If

  End Function  ' OnMenuItemSelected


'-----------------------------------------------------------------------------
public Function OnMenuPopup ( menu )
'-----------------------------------------------------------------------------
  If menu.IsContextMenu() Then
     If ( CStr(menu.ApplicationContext) = "Main" ) Then

'       Add a menu item to the Main context menu ...
        If (Not MenuManager Is Nothing) Then
           Set ContextMenu = MenuManager.MainMenu
           ContextMenu = ContextMenu.Find("Get Data")

           If (nContextMenu = -1) Then
              ContextMenu.InsertSeparator ContextMenu.ItemCount
              FormHandler.G_MyContextMenuID = ContextMenu.InsertItem(ContextMenu.ItemCount, "Get Data")
           Else
             Exit Function
           End If

        End If
     End If
  End If

End Function  ' OnMenuPopup


'-----------------------------------------------------------------------------
' Define Global Variables ...
'-----------------------------------------------------------------------------
  Dim G_MyContextMenuID

End Class  ' MyCMFormHandler


'-----------------------------------------------------------------------------
' Initialization ...
'-----------------------------------------------------------------------------
' Get the Event & Menu Manager objects ...
  Set EventManager = Application.GetObject("EventManager")
  Set MenuManager = Application.GetObject("MenuManager")

' Create a new FormHandler object ...
  Set FormHandler = new MyCMFormHandler

' Set this global variable for first-time-through ...
  G_MyContextMenuID = 0

' Register our event handlers ...
  If (Not EventManager Is Nothing) Then
     EventManager.RegisterHandlerMethod FormHandler, "OnMenuItemSelected"
     EventManager.RegisterHandlerMethod FormHandler, "OnMenuPopup"
  Else
'    Could not get EventManager object.
  End If

]]>
</script>
</pss_extension>
Posted by: dgoyette
2011-01-31 08:29:08
2 of 5
#2901
in reply to #2900
Please use the "menu" object that is passed into OnMenuPopup() instead of going through MenuManager.
Posted by: Anatoly
\n\n[/code]\n");
2011-01-31 14:50:21
3 of 5
#2904
in reply to #2901
Thank you Anatoly. I changed the code to what is displayed below.

However ... the debug line that displays the menu.ApplicationContext ALWAYS displays "Main", regardless of what menu or sub-menu is opened. EXCEPT for the top-level Account menu in the menubar. In this case, it displays "Portfolio".

Thus, MY menu item is added to every single menu I go to (LOL).

I'm assuming that menu.ApplicationContext SHOULD be returning the name of each individual context menu, right?

I'm also assuming that menu.IsContextMenu() should ONLY be True when a right-click menu is displayed, yes? However, it's True for every single menu in the program (all menus in the menubar, etc.).

Thank you,

-Don

<pss_extension name="Context Menus" version="1.0">Context Menus
<script language="VBScript">
<![CDATA[

Class MyCMFormHandler


'-----------------------------------------------------------------------------
  public Function OnMenuItemSelected ( id )
'-----------------------------------------------------------------------------
    If id = G_MyContextMenuID Then
      MsgBox "Code would be placed here to perform the necessary " _
           & "function", vbOKOnly, "Message"
      Exit Function
    End If

  End Function  ' OnMenuItemSelected


'-----------------------------------------------------------------------------
  public Function OnMenuPopup ( menu )
'-----------------------------------------------------------------------------
'   Make sure the menu is a context menu ...
    Application.DebugTrace "menu.IsContextMenu() = '" _
      & CStr(menu.IsContextMenu()) & "'"

    If menu.IsContextMenu() Then
      Application.DebugTrace "menu.ApplicationContext = '" _
        & CStr(menu.ApplicationContext) & "'"

      If ( CStr(menu.ApplicationContext) = "Main" ) Then
        Set ContextMenu = menu

'       Add our menu item to the context menu if it doesn't already exist ...
        nContextMenu = ContextMenu.Find("Get Data")

        If (nContextMenu = -1) Then
          ContextMenu.InsertSeparator ContextMenu.ItemCount
          FormHandler.G_MyContextMenuID = ContextMenu.InsertItem(ContextMenu.ItemCount, "Get Data")
        Else
          Exit Function
        End If

      End If
    End If

  End Function  ' OnMenuPopup


'-----------------------------------------------------------------------------
' Define Global Variables ...
'-----------------------------------------------------------------------------
  Dim G_MyContextMenuID

End Class  ' MyCMFormHandler


'-----------------------------------------------------------------------------
' Initialization ...
'-----------------------------------------------------------------------------
' Get the Event & Menu Manager objects ...
  Set EventManager = Application.GetObject("EventManager")
  Set MenuManager = Application.GetObject("MenuManager")

' Create a new FormHandler object ...
  Set FormHandler = new MyCMFormHandler

' Set this global variable for first-time-through ...
  G_MyContextMenuID = 0

' Register our event handlers ...
  If (Not EventManager Is Nothing) Then
    EventManager.RegisterHandlerMethod FormHandler, "OnMenuItemSelected"
    EventManager.RegisterHandlerMethod FormHandler, "OnMenuPopup"
  Else
'   Could not get EventManager object.
  End If

]]>
</script>
</pss_extension>

Posted by: dgoyette
2011-01-31 15:10:20
4 of 5
#2905
in reply to #2904
IsContextMenu() should only return true for right-click menus. Oops, I just checked the code and it returns true whenever the context string is set, which is always. Ok, I'll have to fix that for the next build.

Yes, the ApplicationContext will show Main in most cases, except for the Portfolio menu and the menu when you right-click an account tab.
Posted by: Anatoly
2011-01-31 15:18:02
5 of 5
#2906
in reply to #2905
Thank you Anatoly. Fixing IsContextMenu() will help a lot, in that my context menu item won't be added to every single menu I click on :).

Posted by: dgoyette