How to Override `Prompt_Singular()` Or `Prompt_Plural()` Method default behavior

The method Prompt_Singular Or Prompt_Plural have an default behavior that hide the Grasshopper Dialog when I click button Set One *** or Set Multuply ***. But how Can I Prevent the default behavior when I click the button?

On a parameter class you wrote? For a type of goo you wrote?

@DavidRutten, Yes. I am writing an Parameter that can store or Persisits the info of layer in Rhino. I want to do When I Click the Set One *** Menuitem and the Grasshopper Froms is still show

You have to override the Prompt_Singular() and Prompt_Plural() methods on your parameter class. In there you can do whatever you like.

But It Seem that the Prompt_Singular() and Prompt_Plural() methods of parameter class has default behavior can not be override .

PS: The default behavior is when you click the MenuItem Set One *** Or Set Multi *** the grasshopper dialog will be hide auto.

Well, most parameters override it, so you’re going to have to post some code which doesn’t work.

Hi @DavidRutten the Code is attach below
1、 GH_Layer is an Parameter;
2、 Hu_Layer is an type that wrapper Rhino Layer

Thanks
GH_Layer.cs (4.6 KB) Hu_Layer.cs (6.0 KB)

The following works for me. It shows the window defined in Prompt_Singular() when that menu item is selected. I ran through the code making small changes, just to get a feel for it all.

using System;
using System.Collections.Generic;
using System.Windows.Forms;

using Rhino.DocObjects;

using Grasshopper.Kernel;
using Grasshopper.Kernel.Types;
using GH_IO.Serialization;

namespace CSCECDEC.Plugin.Types
{
  public class Hu_Layer : GH_Goo<Layer>
  {
    public Hu_Layer()
    {
      Value = null;
    }
    public Hu_Layer(string name)
    {
      var doc = Rhino.RhinoDoc.ActiveDoc;
      if (doc != null)
        Value = doc.Layers.FindName(name);

      // doc.Layers.FindByFullPath has been deprecated. However I don't know whether FindName does the same now.
    }
    public Hu_Layer(int index)
    {
      var doc = Rhino.RhinoDoc.ActiveDoc;
      if (doc != null)
        Value = doc.Layers.FindIndex(index);
    }
    public Hu_Layer(Guid id)
    {
      var doc = Rhino.RhinoDoc.ActiveDoc;
      if (doc != null)
        Value = doc.Layers.FindId(id);
    }
    public Hu_Layer(Layer Layer)
    {
      Value = Layer;
    }
    public override IGH_Goo Duplicate()
    {
      return new Hu_Layer(Value);
    }

    public override bool Write(GH_IWriter writer)
    {
      if (Value != null)
        writer.SetInt32("LayerIndex", Value.Index);

      return base.Write(writer);
    }
    public override bool Read(GH_IReader reader)
    {
      if (reader.ItemExists("LayerIndex"))
      {
        var doc = Rhino.RhinoDoc.ActiveDoc;
        if (doc != null)
          Value = doc.Layers.FindIndex(reader.GetInt32("LayerIndex"));
      }
      else
        Value = null;

      return base.Read(reader);
    }

    public override bool IsValid
    {
      get { return Value != null; }
    }
    public override string TypeName
    {
      get { return "Layer"; }
    }
    public override string TypeDescription
    {
      get { return "A Rhino Layer"; }
    }

    public override string ToString()
    {
      if (Value is null)
        return "Null layer";
      return $"{Value.Index} - {Value.FullPath}";
    }
    public override bool CastFrom(object source)
    {
      if (source is null)
        return false;

      var doc = Rhino.RhinoDoc.ActiveDoc;

      if (GH_Convert.ToInt32(source, out int integer, GH_Conversion.Both))
      {
        Value = doc?.Layers.FindIndex(integer);
        return Value != null;
      }

      if (GH_Convert.ToGUID(source, out Guid id, GH_Conversion.Both))
      {
        Value = doc?.Layers.FindId(id);
        return Value != null;
      }

      // Probably best to not use Secondary or Both for string conversions, 
      // as *everything* can be converted to a string.
      if (GH_Convert.ToString(source, out string name, GH_Conversion.Primary))
      {
        Value = doc?.Layers.FindName(name);
        return Value != null;
      }

      return false;
    }
    public override bool CastTo<Q>(ref Q target)
    {
      if (Value is null)
        return false;

      if (typeof(Q).IsAssignableFrom(typeof(int)))
      {
        target = (Q)(object)Value.Index;
        return true;
      }
      if (typeof(Q).IsAssignableFrom(typeof(GH_Integer)))
      {
        target = (Q)(object)new GH_Integer(Value.Index);
        return true;
      }

      if (typeof(Q).IsAssignableFrom(typeof(Guid)))
      {
        target = (Q)(object)Value.Id;
        return true;
      }
      if (typeof(Q).IsAssignableFrom(typeof(GH_Guid)))
      {
        target = (Q)(object)new GH_Guid(Value.Id);
        return true;
      }

      if (typeof(Q).IsAssignableFrom(typeof(string)))
      {
        target = (Q)(object)Value.FullPath;
        return true;
      }
      if (typeof(Q).IsAssignableFrom(typeof(GH_String)))
      {
        target = (Q)(object)new GH_String(Value.FullPath);
        return true;
      }

      if (typeof(Q).IsAssignableFrom(typeof(Layer)))
      {
        // This is different from before, a new layer was 
        // extracted from the active doc based on the index of the
        // current value.
        target = (Q)(object)Value;
        return true;
      }

      return false;
    }
  }
  public class GH_Layer : GH_PersistentParam<Hu_Layer>
  {
    public GH_Layer()
      : base(new GH_InstanceDescription("Layer", "Lay", "A parameter for referencing a Rhino layer", "Params"))
    { }

    public override Guid ComponentGuid
    {
      get { return new Guid("878c2318-5294-41d9-98f9-2ae90c485afc"); }
    }
    public override GH_Exposure Exposure
    {
      get { return GH_Exposure.primary; }
    }
    protected override Hu_Layer InstantiateT()
    {
      return new Hu_Layer();
    }

    protected override GH_GetterResult Prompt_Singular(ref Hu_Layer value)
    {
      var doc = Rhino.RhinoDoc.ActiveDoc;
      if (doc is null) return GH_GetterResult.cancel;

      var form = new Form
      {
        Text = "Pick a layer",
        Width = Grasshopper.Global_Proc.UiAdjust(200),
        Height = Grasshopper.Global_Proc.UiAdjust(300),
        ShowInTaskbar = false,
        MaximizeBox = false,
        MinimizeBox = false,
        StartPosition = FormStartPosition.CenterParent
      };

      var list = new ListBox
      {
        MultiColumn = false,
        Dock = DockStyle.Fill,
        SelectionMode = SelectionMode.One
      };
      foreach (var layer in doc.Layers)
        list.Items.Add(layer);

      var button = new Button
      {
        Text = "OK",
        Height = Grasshopper.Global_Proc.UiAdjust(32),
        Dock = DockStyle.Bottom,
        DialogResult = DialogResult.OK
      };

      form.Controls.Add(list);
      form.Controls.Add(button);

      if (form.ShowDialog() == DialogResult.OK)
      {
        var layer = list.SelectedItem as Layer;
        value = new Hu_Layer(layer);
        return GH_GetterResult.success;
      }
      else
        return GH_GetterResult.cancel;
    }
    protected override GH_GetterResult Prompt_Plural(ref List<Hu_Layer> values)
    {
      // Same as Prompt_Singular, just allow for multiple selection.
      return GH_GetterResult.cancel;
    }
  }
}
1 Like

Hi David,I’m So Sorry, Maybe I am not exaplain clearly.

I mean when I click the menu item the Grasshopper Main Windows is hide. How Can I override the default behavior?