When developing apps in C #, you usually need multiple forms! Sometimes in order to open the form to prohibit the operation of the parent form, we generally use modal dialog box method, also is to use the ShowDialog () method.
If you have two forms a and b,a open the B form as the main form using the ShowDialog () method, we may experience the following scenarios.
1. Transfer values from A to form B, this generally has two methods. Method One:
A form code:
FORMB fb=New FORMB (); FB. varstr="I am a test string": FB. ShowDialog ()
B Form code:
Public string Varstr;
This allows the content form in form A to be in the B form, using the VARSTR variable directly in the B form.
Method Two:
A form code:
string varstr="aaaaaaaaaa"new FORMB (VARSTR); FB. ShowDialog ();
B Form code:
Private string str; Public FORMB (string str) { InitializeComponent (); this. str=str;}
This can also achieve the same purpose;
2. Transfer values from form B to form a.
This demand is very interesting, Baidu found thousands of ways, a pain than an egg, a more complex than a. Actually, this is really simple.
A form code:
Fromb fb=New FORMB (); FB. ShowDialog (); string rtstr=fb.returnstr;
B Form code:
Public string returnstr;
returnstr=" I am going to pass the content to the a form "; this. Close ();
In fact, according to my own experience, so many people did not think that this is expected to be the B form close () after the destruction, in fact, I found no after the test, close can still get the variables.
C # ShowDialog When the form of a cheap way to pass