winforms - Communicate between two windows forms in C# -


i have 2 forms, 1 main form , other options form. example user clicks on menu on main form: tools -> options, cause options form shown.

my question how can send data options form main form? know use properties, have lot of options , seems tedious odd thing do.

so best way?

form1 triggers form2 open. form2 has overloaded constructor takes calling form argument , provides reference form2 members. solves communication problem. example i've exposed label property public in form1 modified in form2.

with approach can communication in different ways.

download link sample project

//your form1

public partial class form1 : form {     public form1()     {         initializecomponent();     }      private void button1_click(object sender, eventargs e)     {         form2 frm = new form2(this);         frm.show();     }      public string labeltext     {         { return lbl.text; }         set { lbl.text = value; }     } } 

//your form2

public partial class form2 : form {     public form2()     {         initializecomponent();     }      private form1 mainform = null;     public form2(form callingform)     {         mainform = callingform form1;          initializecomponent();     }      private void form2_load(object sender, eventargs e)     {      }      private void button1_click(object sender, eventargs e)     {         this.mainform.labeltext = txtmessage.text;     } } 

alt text http://demo.ruchitsurati.net/files/frm1.png

alt text http://demo.ruchitsurati.net/files/frm2.png


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -