C # ShowDialog When the form of a cheap way to pass

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.