Persistent data not updated from ToolStrip Menu

Hello,

I have a custom parameter to select an option from the drop down menu. This is the code:

public class gh_param_enumOption : GH_PersistentParam<gh_goo_enumOption>
{
private EventHandler OnMenuClick;

public gh_param_enumOption() : base(new GH_InstanceDescription("Option", "Option", "", "Test"))
{
    OnMenuClick += MenuItemClick;
}
public override Guid ComponentGuid
{
    get
    {
        return new Guid("94301CA0-8CCF-4B80-8C56-C23DD29D5EA2");
    }
}
public override void AppendAdditionalMenuItems(ToolStripDropDown menu)
{
    base.AppendAdditionalMenuItems(menu);
    var names = Enum.GetNames(typeof(Test.enumOption));
    var values = Enum.GetValues(typeof(Test.enumOption));
    for (int index = 0; index < names.Length; index++)
    {
        var item = menu.Items.Add(names[index], null, OnMenuClick);
        item.Tag = values.GetValue(index);
    }
}
protected override GH_GetterResult Prompt_Plural(ref List<gh_goo_enumOption> values)
{
    return GH_GetterResult.cancel;
}
protected override GH_GetterResult Prompt_Singular(ref gh_goo_enumOption value)
{
    return GH_GetterResult.cancel;
}
private void MenuItemClick(object sender, EventArgs e)
{
    var item = (ToolStripItem)sender;
    if (item == null) return;
    PersistentData.Clear();
    Test.enumOption option = (Test.enumOption)((int)item.Tag);
    SetPersistentData(new gh_goo_enumOption(option));            
}

}

The changes are not being update in Grasshopper until F5 is pressed… Can you help me?

I actually recommend deriving from Param_Integer and setting a bunch of named values in the constructor. It’s less code, and keeps the parameter more consistent with similar parameters in GH. You’ll also notice that in this case the tooltip actually showes names rather than integer values, which is more user friendly.

However, the attached file also contains an example about how to do this by populating your own menu and handling the click events. namedparameters.cs (2.8 KB)

    public class gh_param_enumOption : GH_PersistentParam<GH_Integer>
    {
      public gh_param_enumOption()
        : base(new GH_InstanceDescription("Option", "Option", "", "Test", "Test"))
      { }

      public static readonly Guid _id = new Guid("94301CA0-8CCF-4B80-8C56-C23DD29D5EA2");
      public override Guid ComponentGuid
      {
        get
        {
          return _id;
        }
      }

      public override void AppendAdditionalMenuItems(ToolStripDropDown menu)
      {
        base.AppendAdditionalMenuItems(menu);
        Menu_AppendSeparator(menu);

        var names = Enum.GetNames(typeof(Rhino.Geometry.BlendContinuity));
        var values = Enum.GetValues(typeof(Rhino.Geometry.BlendContinuity));
        for (int i = 0; i < names.Length; i++)
        {
          ToolStripMenuItem item = Menu_AppendItem(menu, names[i], EnumItemClick, null, true, false);
          item.Tag = values.GetValue(i);
        }

        Menu_AppendSeparator(menu);
      }
      private void EnumItemClick(object sender, EventArgs eventArgs)
      {
        ToolStripMenuItem item = sender as ToolStripMenuItem;
        if (item == null)
          return;

        if (item.Tag is Rhino.Geometry.BlendContinuity)
        {
          Rhino.Geometry.BlendContinuity c = (Rhino.Geometry.BlendContinuity) item.Tag;
          PersistentData.Clear();
          PersistentData.Append(new GH_Integer((int)c), new GH_Path(0));
          ExpireSolution(true); // This will update the state of the document. Basically, F5 calls this methods for ALL objects on the canvas.
        }
      }

      protected override GH_GetterResult Prompt_Plural(ref List<GH_Integer> values)
      {
        return GH_GetterResult.cancel;
      }
      protected override GH_GetterResult Prompt_Singular(ref GH_Integer value)
      {
        return GH_GetterResult.cancel;
      }
    }
    public class gh_param_namedOption : Param_Integer
    {
      public gh_param_namedOption()
      {
        Name = "Named Option";
        NickName = "NOption";
        Category = "Test";
        SubCategory = "Test";

        var names = Enum.GetNames(typeof(Rhino.Geometry.BlendContinuity));
        var values = Enum.GetValues(typeof(Rhino.Geometry.BlendContinuity));
        for (int i = 0; i < names.Length; i++)
        {
          string name = names[i];
          int value = (int)values.GetValue(i);
          AddNamedValue(name, value);
        }
      }

      public static readonly Guid _id = new Guid("2D0CDEC3-FE26-4401-815B-EB993735EC29");
      public override Guid ComponentGuid
      {
        get
        {
          return _id;
        }
      }

      protected override GH_GetterResult Prompt_Plural(ref List<GH_Integer> values)
      {
        return GH_GetterResult.cancel;
      }
      protected override GH_GetterResult Prompt_Singular(ref GH_Integer value)
      {
        return GH_GetterResult.cancel;
      }
    }

Thanks, I’ll do it that way.