Question about the Menu_AppendItem Method


#1

Hi,

I was trying to use the above named method. My question is how to set the image in the (ToolStrip, String, EventHandler, Image, Boolean, Boolean) overload just when the item is selected like int he Bounding Box native component.
My attempt so far with the use of the explanation from James Ramsden here: http://james-ramsden.com/append-menu-items-to-grasshopper-components-with-c/

   protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
        {
        }




        protected override void AppendAdditionalComponentMenuItems(System.Windows.Forms.ToolStripDropDown menu)
        {


            base.AppendAdditionalComponentMenuItems(menu);
            Menu_AppendItem(menu, "Flip myBool", Menu_DoClick, ToolTipTest.Properties.Resources.arcArrayIconNeu,false);
            base.AppendAdditionalComponentMenuItems(menu);
            Menu_AppendItem(menu, "Flip myBool", Menu_DoClick, ToolTipTest.Properties.Resources.arcArrayIconNeu, false);
            
        }

        private void Menu_DoClick(object sender, EventArgs e)
        {
            myBool = !myBool;
        }
        public bool myBool = false; 





        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
        {
        }

        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object can be used to retrieve data from input parameters and 
        /// to store data in output parameters.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            if (myBool == true)
            {
                Message = ("1");
            }


            else
            {
                Message = ("2");
            }


            ExpireSolution(true);
        }

Thanks for everybody taking time looking on it.

Regards!


#2

You have to subscribe to the event and change the image from that context. The sender is the object that triggers the event, so you can convert it to some type of the ToolStrip family and change the image with its Image property.


#3

Sorry @Dani_Abalde,

I dont get it. How can I subscribe to the event and convert it to some member of the tooltip family?

Thanks for your time!


#4

Menu_AppendItem(menu, “Flip myBool”, Menu_DoClick, ToolTipTest.Properties.Resources.arcArrayIconNeu,false);

private void Menu_DoClick(object sender, EventArgs e)
{
     if(sender is ToolStripMenuItem){ //ToolStripMenuItem type if I remember correctly
         ToolStripMenuItem item = sender as ToolStripMenuItem;
         item.Image = whatever;
     }
}

(David Rutten) #5

There’s no point assigning an image to a menu item from within the click event handler. The menu is about to be closed (or has already been closed) and all items will be discarded. You must assign the correct item image when you add the item to the menu.


#6

Ouuh, true… then it would be like this, wouldn’t it?

Bitmap img = myBool ? ToolTipTest.Properties.Resources.arcArrayIconNeu : null;
Menu_AppendItem(menu, "Flip myBool", Menu_DoClick, img, false);

(David Rutten) #7

Yup, or you can just use the checked property of the menu to indicate on/off state, that is the default windows style. Using custom images is cool, but may also confuse people unless it’s very clear which image means what.


#8

Works perfect!

Muchas gracias Dani!