How to properly implement Menu_AppendItem to store the menu item state

Hello everyone,

I am wondering how to properly implement Menu_AppendItem and also ensure that the menu item state is saved when I saved the grasshopper file. My current code below is hacky at best! Thanks in advance!

        private bool multithreadingButton = true;

        private void Menu_ItemClicked(Object sender, EventArgs e)
        {
            if (!multithreadingButton)
            {
                this.Message = "Multi-Threading";
                multithreadingButton = true;
                this.ExpireSolution(true);
            }
            else
            {
                this.Message = "Normal";
                multithreadingButton = false;
                this.ExpireSolution(true);

            }

        }

        public override void AppendAdditionalMenuItems(ToolStripDropDown menu)
        {
            base.AppendAdditionalMenuItems(menu);
            if (!multithreadingButton)
                Menu_AppendItem(menu, "Multi-Threading", Menu_ItemClicked, true, false);
            else
                Menu_AppendItem(menu, "Multi-Threading", Menu_ItemClicked, true, true);

        }

That’s pretty much how I implement the additional menu items. To save the data you will need to overwrite the GH_Component.Write method and GH_Component.Read method the serialize and deserialize the data. This should show you the basics of serialization if you don’t already know, though as it is a component be sure to call the base method.

2 Likes