Component parameter as a Number

Hi,

Is there a way to pass a number by right click of the grasshopper component in AppendAdditionalComponentMenuItems ?

I have only seen boolean parameters, but numbers would be really helpful.

Could someone show an example of such implementation?

namespace MenuInput
{
  public class MenuInputComponent : GH_Component
  {
      private double _number;
      public MenuInputComponent() : base("MenuInput", "Nickname",
          "Description", "Category", "Subcategory")
      { }
      protected override void RegisterInputParams(GH_InputParamManager pManager) { }
      protected override void RegisterOutputParams(GH_OutputParamManager pManager)
      {
          pManager.AddNumberParameter("O", "O", "O", GH_ParamAccess.item);
      }
      protected override void SolveInstance(IGH_DataAccess DA)
      {
          DA.SetData(0, _number);
      }
      public override void AppendAdditionalMenuItems(ToolStripDropDown menu)
      {
          var setInt = Menu_AppendItem(menu, "Set Number");
          Menu_AppendTextItem(setInt.DropDown, _number.ToString(CultureInfo.InvariantCulture), null, TextChanged, true);
      }
      private void TextChanged(GH_MenuTextBox sender, string newText)
      {
          _number = Convert.ToDouble(newText);
          ExpireSolution(true);
      }
      protected override System.Drawing.Bitmap Icon => null;
      public override Guid ComponentGuid => new Guid("d2bc99d5-36b1-4139-b42d-f21ac50d3a90");
  }
}

MenuInput.zip (22.5 KB) MenuInput.gha (6 KB)

1 Like

That is super useful, thank you so much, works like a charm.

Is there a way to access those two click buttons?
I’d like to NOT recompute every time the text changes
I only want the text change to take effect when one clicks the commit changes

ps. I’m aware that I could use Windows.Forms classes and script the above behavior myself. Just curious.

namespace MenuInput
{
  public class MenuInputComponent : GH_Component
  {
    private double _number;
    private string _text;
    public MenuInputComponent() : base("MenuInput", "Nickname", "Description", "Category", "Subcategory") { }
    protected override void RegisterInputParams(GH_InputParamManager pManager) { }
    protected override void RegisterOutputParams(GH_OutputParamManager pManager)
    {
       pManager.AddNumberParameter("O", "O", "O", GH_ParamAccess.item);
    }
    protected override void SolveInstance(IGH_DataAccess DA)
    {
       DA.SetData(0, _number);
    }
    public override void AppendAdditionalMenuItems(ToolStripDropDown menu)
    {
       var setInt = Menu_AppendItem(menu, "Set Number");
       Menu_AppendTextItem(setInt.DropDown, _mber.ToString(CultureInfo.InvariantCulture), null, TextChanged, true);
       setInt.DropDownItems[1].Click += CommitChanges;
    }
    private void TextChanged(GH_MenuTextBox sender, string newText)
    {
       _text = newText;
    }
    private void CommitChanges(object sender, EventArgs e)
    {
        if (RhinoMath.EpsilonEquals(Convert.ToDouble(_text), _number, double.Epsilon)) return;
        _number = Convert.ToDouble(_text);
        ExpireSolution(true);
     }
    protected override System.Drawing.Bitmap Icon => null;
    public override Guid ComponentGuid => new Guid("d2bc99d5-36b1-4139-b42d-f21ac50d3a90");
  }
}

MenuInput.zip (23.2 KB) MenuInput.gha (6.5 KB)

1 Like

very nice!
sneaky Menu_AppendTextItem actually adds two more menu items… :upside_down_face: