How to pass the data from form to component?


(Andrealu2012) #1

i have a component,and then double click it, a windowform show,and waiting for input data,then click button to close the window and pass the data to component.


How to pass the data to the component?

thankyou!
this is code…
winForm.rar (31.2 KB)


How to perserve data in gh component?
#2

Asign input data to a public variable in your comp and recompute the component.
Output that variable in SolveInstance.


(Andrealu2012) #3

i try …but failed…:slightly_frowning_face:

another thing is:i donnot want to use static class and static member to pass data between the form and component,if do so, if you have a lot of this component in one gh file ,i am afraid it will share the same value…


#4

Keep trying.
Or put the code here in a comment.


(Andrealu2012) #5

Component code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Rhino.Geometry;
using Rhino;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Rhino.Display;
using System.Drawing;
using Grasshopper.Kernel.Types;
using GH_IO.Serialization;

using Grasshopper.GUI.Canvas;
using Grasshopper.GUI;
using Grasshopper.Kernel.Attributes;

namespace testf4goo
{
public class TestWinFormAttributes:GH_ComponentAttributes
{
public TestWinFormAttributes(TestWinForm owner) : base(owner) { }
public override Grasshopper.GUI.Canvas.GH_ObjectResponse RespondToMouseDoubleClick(GH_Canvas sender,GH_CanvasMouseEvent e)
{

        Form1 f1 = new Form1();
        Grasshopper.GUI.GH_WindowsFormUtil.CenterFormOnCursor(f1, true);
        f1.ShowDialog();


        Owner.ExpireSolution(true);

        
        return Grasshopper.GUI.Canvas.GH_ObjectResponse.Handled;

    }
    public override void ExpireLayout()
    {
        base.ExpireLayout();
    }

}



public class TestWinForm : GH_Component
{
    /// <summary>
    /// Initializes a new instance of the TestWinForm class.
    /// </summary>
    public TestWinForm()
      : base("TestWinForm", "WF",
          "Description",
          "Test", "Test")
    {
    }
    public override void CreateAttributes()
    {
        // base.CreateAttributes();
        m_attributes = new TestWinFormAttributes(this);

    }
    /// <summary>
    /// Registers all the input parameters for this component.
    /// </summary>
    protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
    {
        pManager.AddNumberParameter("Input", "I", "inputNum", GH_ParamAccess.item,0);

    }

    /// <summary>
    /// Registers all the output parameters for this component.
    /// </summary>
    protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
    {
        pManager.AddNumberParameter("Output", "O", "outputNum", GH_ParamAccess.item);
    }

    public double B
    {
        set; get;
    } = 0;
    /// <summary>
    /// This is the method that actually does the work.
    /// </summary>
    /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
    protected override void SolveInstance(IGH_DataAccess DA)
    {
        double A=0;
        DA.GetData("Input", ref A);
        //double B = 0;
        double C = B+A;

        Message = $"result is :{C}";
        DA.SetData("Output", C);

    }

    /// <summary>
    /// Provides an Icon for the component.
    /// </summary>
    protected override System.Drawing.Bitmap Icon
    {
        get
        {
            //You can add image files to your project resources and access them like this:
            // return Resources.IconForThisComponent;
            return null;
        }
    }

    /// <summary>
    /// Gets the unique ID for this component. Do not change this ID after release.
    /// </summary>
    public override Guid ComponentGuid
    {

        get { return new Guid("9F8A0403-01EA-469D-B91F-02165B952503"); }
    }
}

}


form1 code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Rhino.Geometry;
using Rhino;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Rhino.Display;
using Grasshopper.Kernel.Types;
using GH_IO.Serialization;

using Grasshopper.GUI.Canvas;
using Grasshopper.GUI;
using Grasshopper.Kernel.Attributes;

namespace testf4goo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

        this.ControlBox = false;


    }




    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void Button1_Click(object sender, EventArgs e)
    {
        //the instance of TestWinForm's data member = textBox1.Text;
        this.Close();
    }
}

}


(Chris Hanley) #6

Maybe this discussion could help?


#7

You need a public member, not an static member.

Create a public member in your comp:
class TestWinForm: GH_Component{
public int UserData;
}

Make a constructor to pass your comp in the form.

public partial class Form1 : Form
{
TestWinForm comp;
public Form1(TestWinForm Owner) : this(){
comp = Owner;
}

And in the call, from attributes:
Form1 f1 = new Form1(this.Owner as TestWinForm);

private void Button1_Click(object sender, EventArgs e)
{
comp.UserData = textBox1.Text ; // Cast to int !
comp.ExpireSolution(true);
this.Close();
}


(Andrealu2012) #8

Thankyou Dani,it works ,thank you!