.net - passing data between 3 windows forms in visual studio using C# -
i have windows application has 3 forms : form1,2,3. want send text of textbox form2
form1
, same text form1
form3
, is,
text form2
-->form1
-->form3
- form 1, has 2 buttons , openform2, openform3.
- form2 has textbox form2_textbox, & button send_to_form1_button
- form3 has textbox received_from_form1_textbox
now,
- on clicking button
openform2
onform1
,form2
opens, - a string entered in textbox
form2_textbox
ofform2
, - when button
form2_button
of form clicked, wantform1
receive string value & stores in stringreceivefromform2
, - and displays string value on
form3_textbox
ofform3
.
public partial class form1 : form { string receivefromform2a; public form1() { initializecomponent(); } public void method_receive_from_form2(string receivefromform2) { receivefromform2a = receivefromform2; form3 f3 = new form3(receivefromform2a); } private void openform3_click(object sender, eventargs e) { form3 f3 = new form3();**----this line gives error:no overload method form3 takes 0 arguments** f3.show(); } private void openform2_click(object sender, eventargs e) { form2 f2 = new form2(); f2.show(); } }
public partial class form2 : form { public form2() { initializecomponent(); string loginname = form2_textbox.text; } //sending value of textbox on form2 form1. private void send_to_form1_button_click(object sender, eventargs e) { form1 f1 = new form1(); f1.method_receive_from_form2(form2_textbox.text); } }
public partial class form3 : form { public form3(string receive_from_form1) { initializecomponent(); received_from_form1_textbox.text = receive_from_form1; } }
this error occurs because in form2
have given argument form1
during object creation. should do? there other way or how remove error?
when include f3.show()
in method method_receive_from_form2 there no error. makes form3
load automatically without button click. want form3
open clicking button on form1
. , value shown in textbox.
i recommend change using constructors using properties. keep things 'contained' , it's pretty simple.
ex:
public partial class form3 : form { public string form1text {get; set;} public form3() { initializecomponent(); } }
public partial class form2 : form { public form2() { initializecomponent(); string loginname = form2_textbox.text; } public string form2text {get; set;} private void send_to_form1_button_click(object sender, eventargs e) { form2text = form2_textbox.text; this.dialogresult = dialogresult.ok; this.close(); } }
then in form 1:
public partial class form1 : form { string receivefromform2a; public form1() { initializecomponent(); } private void openform3_click(object sender, eventargs e) { form3 f3 = new form3(); f3.form1text = receivefromform2a; f3.show(); } private void openform2_click(object sender, eventargs e) { form2 f2 = new form2(); if(f2.showdialog() == dialogresult.ok) { receivefromform2a = f2.form2text; //new property on form2. } } }
Comments
Post a Comment