How to pass data from Winform to the component's exit


(C18957181678) #1

I create a Component with Visual studio, which include a class (use NewGrasshopperTemplate to creat) and a Form. the Form was started by clicking menu in the class. When the data in the Form is calculated, how to output the calculated data to the output of the Component?

another question:

code here:

     public override bool AppendMenuItems(ToolStripDropDown menu)
       {
           Menu_AppendGenericMenuItem(menu, "select layer",first_item_click, 
           Properties.Resources.Icon1,true);

           return true;
       }

    private void Menu_AppendGenericMenuItem(ToolStripDropDown menu, string v1, Action<object, EventArgs> first_item_click, Icon icon1, bool v2)
    {
        throw new NotImplementedException();
    }

question: when the Component was Compiled and intalled in GH, when right click, the Component didn’t popup menu, why?

thanks.


How to pass the data from form to component?
(David Rutten) #2

Is your form shown modally, or is it mode-less?

Because you’re throwing exceptions in your menu population code. Remove throw new NotImplementedException(); and it should work.


(C18957181678) #3
  1. in form, I declare : public List inputlist;

    in class:

    protected override void SolveInstance(IGH_DataAccess DA)
     {    
         if ((!DA.GetDataList(0, instring)))   return;
         Form1 form = new Form1();
         form.inputlist = instring;
     }
    
    
    private void item_click(Object sender, EventArgs e)
     {
         Form1 form = new Form1();
         form.ShowDialog();  
     }
    

    the question : I can’t transfer data to “DA.SetData(0, ))”, I don’t know how to put the data to output of component.

  2. yes,the menu question was solved, but there was a warning:This method is obsolete.

thanks you very much!!


(David Rutten) #4

That means it’s modal, i.e. the rest of the UI is locked until the window is closed, and the next line of code to execute after the window is closed is the line after form.ShowDialog(). If you had used Show() instead then the code continues on running right away and the window does not lock the UI. The modal way is easier to deal with because you don’t have to worry about events or callbacks.

After form.ShowDialog(); you must harvest the new values in the form class and apply them to your component. Probably you’d store them as a class level variable, then read that variable from within SolveInstance().

You must also trigger a new solution once the form closes, which can be done via ExpireSolution(true).

I’d recommend against creating forms inside SolveInstance, you should create the form only just before you need it and dispose of it when you’re done. That means I imagine the code might look a bit like this:

public class MyComponent : GH_Component
{
  private TypeA _valueA;
  private TypeB _valueB;
  ...
  protected override void SolveInstance(IGH_DataAccess DA)
  {
    ...
    DA.SetData(0, _valueA);
    DA.SetData(1, _valueB);
  }

  public override bool AppendMenuItems(ToolStripDropDown menu)
  {
    Menu_AppendGenericMenuItem(menu, "select layer", LayerClick, Properties.Resources.Icon1, true);
    return true;
  }

  private void LayerClick(object sender, EventArgs e)
  {
    Form1 form = new Form1();
    DialogResult rc = form.ShowDialog();
    if (rc != DialogResult.OK)
      return;

    _valueA = form.GetValueA();
    _valueB = form.GetValueB();
    form.Dispose();
    ExpireSolution(true);
  }
}

(C18957181678) #5

thanks David , i 'll try it .