Submenus in ContextMenu

Hi!

I am a complete newbie in .NET C# and SDL Trados APIs, so sorry for my ignorance in advance. :)

I was able to add a new menu item into context menu in Studio editor for calling my own Action. Since I want to maintain some user-specific (plugin) settings, I created a form to save some connection information into Settings.Default.... Now I want to be able to open the settings form anytime and the best place (for me) seems to be from a submenu of my plugin in ContextMenu.

So, please, could you tell me how to add such a subitem into the context menu or where is a better place to put a button to open the settings form?

 

And be prepared, other questions will follow soon. :)

And thanks for the great opportunity to write plugins for SDL Trados with the APIs!

Parents
  • Hi Vít,

    it's now 2018 and the API still seems to have no solution for this. I have now "solved" it with this work around:

    1. Create a borderless fairly small form which doesn't show in the taskbar.
    2. Put a ContextMenuStrip on it which covers the whole form (hence the small form)
    3. When your ContextMenu action is triggered show the form:
    protected override void Execute()
    {
    ContextMenuForm menu = new ContextMenuForm ();
    menu.Show();
    }

    You may fill the ContextMenuStrip dynamically or have a static ContextMenuStrip.

    Obviously, positioning of the window is a bit of a problem. As a quick fix I took the current mouse position (GetCursorPos in user32.dll) and offset it by 20 x -25.

    public ContextMenuForm ()
    {
    InitializeComponent();
    InitContextMenu();

    this.StartPosition = FormStartPosition.Manual;

    this.Location = NativeMethods.GetCursorPosition(); // uses GetCursorPos in user32.dll
    this.Location.Offset(20, -25);
    }

    When the form is shown you also show the ContextMenuStrip (by using a ContextMenuStrip instead of just a form you can be quite sure it won't be off screen):
    protected override void OnShown(EventArgs e)
    {
    base.OnShown(e);
    this.contextMenuStrip1.Show(this.Location);
    }

    Finally, don't forget to close the form, eg. after clicking an item or losing the focus.

    Happy coding
    Angela
Reply
  • Hi Vít,

    it's now 2018 and the API still seems to have no solution for this. I have now "solved" it with this work around:

    1. Create a borderless fairly small form which doesn't show in the taskbar.
    2. Put a ContextMenuStrip on it which covers the whole form (hence the small form)
    3. When your ContextMenu action is triggered show the form:
    protected override void Execute()
    {
    ContextMenuForm menu = new ContextMenuForm ();
    menu.Show();
    }

    You may fill the ContextMenuStrip dynamically or have a static ContextMenuStrip.

    Obviously, positioning of the window is a bit of a problem. As a quick fix I took the current mouse position (GetCursorPos in user32.dll) and offset it by 20 x -25.

    public ContextMenuForm ()
    {
    InitializeComponent();
    InitContextMenu();

    this.StartPosition = FormStartPosition.Manual;

    this.Location = NativeMethods.GetCursorPosition(); // uses GetCursorPos in user32.dll
    this.Location.Offset(20, -25);
    }

    When the form is shown you also show the ContextMenuStrip (by using a ContextMenuStrip instead of just a form you can be quite sure it won't be off screen):
    protected override void OnShown(EventArgs e)
    {
    base.OnShown(e);
    this.contextMenuStrip1.Show(this.Location);
    }

    Finally, don't forget to close the form, eg. after clicking an item or losing the focus.

    Happy coding
    Angela
Children
No Data