winforms - how to pass a values from one form to another form without creating new instance of a form in c#? -


ya aware calling form quite common requirement using

form3 f3 = new form3();
f3.showdialog();

difficulty facing don't want create new instance of form3 every time , can't create constructor globally passing parameters it.
requirement form1 textbox value should passed form3 datagrid.
how that?
form1 code is

    string l=null;     string m= null;     decimal n=0;     string o=null;     private void form1_mouseup(object sender, mouseeventargs e)     {                                this.cursor = cursors.default;         class1 = new class1(mold, mcur, mshape, mwidth, mcolor);         ds.add(a);         int c = (mold.x > mcur.x ? mcur.x : mold.x);         int b = (mold.y > mcur.y ? mcur.y : mold.y);         int w = math.abs(mold.x - mcur.x);         int h = math.abs(mold.y - mcur.y);         string line = layername.text + "\t" + material.text+ "\t" + w.tostring() + "\t" + h.tostring() + "\t" + c.tostring() + "\t" + b.tostring() + "\t" + numericupdown1.value + "\t" + combobox3.text + environment.newline;          l=layername.text;          m=material.text;          n=numericupdown1.value;          o=combobox3.text;          form3 f3 = new form3(l, m, n, o);          f3.showdialog();         string path = @"c:\users\pri\desktop\efg.txt";         if (!file.exists(path))         {             file.create(path);             textwriter tw = new streamwriter(path);             tw.writeline(line);             tsw.close();         }         else if (file.exists(path))         {             textwriter tw = new streamwriter(path, true);             tw.writeline(line);             tw.close();         }       } 

form3:

    public form3(string mystring, string rek, decimal myvalue, string text1)     {         initializecomponent();          string[] rows = new string[2];          (int = 0; < rows.length; i++)         {             datagridviewrow row = new datagridviewrow();             row.createcells(datagridview1);             if (mystring != null)                 datagridview1.rows[i].cells[0].value = mystring;             if (rek != null)                 datagridview1.rows[i].cells[1].value = rek;             if (myvalue != 0)                 datagridview1.rows[i].cells[2].value = myvalue;             if (text1 != null)                 datagridview1.rows[i].cells[3].value = text1;             // int index = this.datagridview1.rows.count;              datagridview1.rows.add(rows);         }     } 

problem:the textbox values replacing in same row everytime without updating in new row.

1st way:

in form 1,

public partial class form1 : form {     public form1()     {         initializecomponent();     }     private void button1_click(object sender, eventargs e)     {         var frm2 = new form2(datagridview1.rows[0].cells[0].value.tostring());         frm2.show();     } } 

form2

public partial class form2 : form {     public form2(string s)     {         initializecomponent();         textbox1.text = s;     } } 

2nd way:

public event eventhandler<form2eventargs> form2event;  public class form2eventargs : eventargs {    public object data {get;set;} }  event listener in form1:  private void getdata(object sender, form2eventargs args) {     object data = args.data;  }   calling event,     form2 form2 = new form2();    form2.form2event += getdata;    if(form2event != null)    form2event(this, new form2eventargs {data = data}); 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -