Adding menu items outside the usual logic

Original Title: Add menu items outside AppendAdditionalComponentMenuItems. (not accepted by the forum -> “Title seems unclear, is it a complete sentence?”)

Hello,

I’m looking to add entries to a component’s context menu outside of the usual logic via GH_Component.AppendAdditionalComponentMenuItems.

I am updating my C# script debugger (https://github.com/corbane/Ghx.CSharpScript) and would like to be able to add options to the components menu via the CSX script.

My problem is that all of these updates are done by its script and after component initialization, so after calling GH_Component.AppendAdditionalComponentMenuItems.

I tried various things, like:

public class CsxComponent : GH_Component
{
        ToolStripDropDown m_menu;
        protected override void AppendAdditionalComponentMenuItems (ToolStripDropDown menu)
        {
            //...
            m_menu = menu;
        }
        void OnUpdateCodeSource ()
        {
            foreach (var option in m_codeSource) {
                Menu_AppendItem (m_menu, option.Name, ...);
                // I have try here some specific Windows.Forms function like
                // - m_menu.ResumeLayout (true);
                // - m_menu.Refresh ();
            }
        }
}

At runtime, m_menu.Items is assigned, but when Grasshopper is refreshed, the new item is deleted.

I know this is a very special case but if anyone has any idea how to fix this that would be really great !!

thanks, jmv.

This is not possible because the context menu is recreated each time it is displayed.

We are lucky … big brother Discourse watchs over us

Yes, that’s true, but in the developer forum and for future research, “Adding menu items outside of AppendAdditionalComponentMenuItems” made more sense.

Discourse doesn’t like “AppendAdditionalComponentMenuItems”.

Sure, it was sarcastic … I think that was not clear. :confused:

Not allowing to write routine names makes little sense when talking about programming IMO. :slight_smile:

Every context menu is created afresh each time it is shown. Caching a menu instance will get you nowhere.

You’ll need to store the information regarding the custom items somewhere central, and add them from within AppendAdditionalComponentMenuItems.

Thank you for your reply.
Yes, it took me a long time to understand this.
it’s absurd. I imagined that all the context menu was kept in memory …

There’s too many of them, and all menus and items take up GDI handles of which there’s only a limited amount available.

Like Emilio, I may not have been clear.
Yes, it makes sense not to keep all of these things in memory.

Me, I don’t know why I imagined this … :slight_smile:

1 Like