New slider by c++

Hello; i find a code how to add a new slider with c++
but i got problem with line 67 PointF

That’s C#, not C++.

PointF is part of the System.Drawing namespace, you’ll have to either add that using statement at the top of the script or use the full name of the type in the code:

new System.Drawing.PointF(...);

1 Like

Also the code this.Component.Attributes.DocObject.Attributes.Bounds.Left can be reduced to just Component.Attributes.Bounds.Left

1 Like

Thank you very much

:smile: i have no idea about that
the code now work fine ; how i can extract Values of sliders: A=x ?

x is a bool in your script. I’m not sure what you mean by “extract” the value. Unless you mean A=y;…

Incidentally the last two lines in that script definitely shouldn’t be running inside a RunScript block. They are making changes to the document while a solution is running. That is illegal, and if it doesn’t throw up errors you just got lucky.

Once I get to the office I can show you how to do this properly.

1 Like

Yes i mean y ; thank you David

@seghierkhaled does this help?Slider Instantiate.gh (3.5 KB)

1 Like

This is how it should have been done: AddSlider.gh (6.9 KB)

private void RunScript(bool Create, List<double> Input, ref object Output)
{
  Output = Input;

  // Schedule another solution to start 5 milliseconds after the
  // current one completes and that new solution will first call
  // the 'AddAnotherSlider' method.
  if (Create && !PreviousCreate)
    GrasshopperDocument.ScheduleSolution(5, AddAnotherSlider);

  PreviousCreate = Create;
}

// <Custom additional code> 
// The PreviousCreate field ensures we don't keep on
// adding sliders when the button is down all the time.
// We only want to add a new slider if the button is down
// AND it didn't used to be down.
private bool PreviousCreate = false;
private void AddAnotherSlider(GH_Document doc)
{
  // Generate a random number in the (0, 10) interval.
  var random = new Random();
  var rValue = random.NextDouble() * 10;

  // Create a new slider and set its limits and value.
  var slider = new GH_NumberSlider();
  slider.SetInitCode(string.Format("0.00..{0:0.00}..10.00", rValue));

  // Make sure the slider attributes exist and are correctly laid out.
  slider.CreateAttributes();
  slider.Attributes.PerformLayout();

  // Compute the position for the slider.
  var input = Component.Params.Input[1];
  var pivot = input.Attributes.InputGrip;
  pivot.X -= slider.Attributes.Bounds.Width + 30;
  pivot.Y += 30 * input.SourceCount;

  // Move the slider and expire its layout.
  slider.Attributes.Pivot = pivot;
  slider.Attributes.ExpireLayout();

  // TODO: we should add an UNDO record for these two operations.

  doc.AddObject(slider, false);
  input.AddSource(slider);
}
// </Custom additional code> 
1 Like

Thank you ; this is useful , i add min|max of sliders just need value output: A = y

Thank you David ; very useful
i will try to combine your script with the script of Mr.A. together

Thank you @DavidRutten & @tognatta.aditya

Slider Instantiate new.gh (11.7 KB)

private void RunScript(bool Enable, int Num, int Min, int Max, List<double> Input, ref object Output)
{
  Output = Input;
  a = Min;
  b = Max;

  if (Enable && z != Num)
    GrasshopperDocument.ScheduleSolution(5, AddAnotherSlider);
  z = Num;
}

// <Custom additional code> 
// The PreviousCreate field ensures we don't keep on
// adding sliders when the button is down all the time.
// We only want to add a new slider if the butter is down
// AND it didn't used to be down.
private int z;
private int a;
private int b;
private void AddAnotherSlider(GH_Document doc)
{
  if(this.Component.Params.Input[4].SourceCount > 0)
  {
    List<IGH_Param> sources = new List<IGH_Param>(this.Component.Params.Input[4].Sources);
    GrasshopperDocument.RemoveObjects(sources, false);
  }
  for(int i = 0;i < z;i++)
    // Generate a random number in the (0, 10) interval.
    //var random = new Random();
    //var rValue = random.NextDouble() * 10;
  {
    // Create a new slider and set its limits and value.
    var slider = new GH_NumberSlider();
    //slider.SetInitCode(string.Format("0.00..{0:0.00}..10.00"));
    slider.Slider.Maximum = b;
    slider.Slider.Minimum = a;
    slider.Slider.DecimalPlaces = 2;

    // Make sure the slider attributes exist and are correctly laid out.
    slider.CreateAttributes();
    slider.Attributes.PerformLayout();

    // Compute the position for the slider.
    var input = Component.Params.Input[4];
    var pivot = input.Attributes.InputGrip;
    pivot.X -= slider.Attributes.Bounds.Width + 30;
    pivot.Y += 30 * input.SourceCount;

    // Move the slider and expire its layout.
    slider.Attributes.Pivot = pivot;
    slider.Attributes.ExpireLayout();

    // TODO: we should add an UNDO record for these two operations.
    doc.AddObject(slider, false);
    input.AddSource(slider);
  }
}

Is it possible to keep the values of the sliders even add or remove ?