简体   繁体   中英

C# Getting textBox value from previous form

I have two forms.

Form A requires the user to input text into two text-boxes, name and number.

Form B gets the texts from the two text-boxes in Form A and displays the texts into two labels.

Right now, when the program is run, the two labels do not displays the texts.

Please help, thanks in advance.

The following are my codes for form B:

        Menu_Privacy_Cleaner_Investigator pci = new Menu_Privacy_Cleaner_Investigator();

        String name = pci.textBoxName.Text;
        String number = pci.textBoxNumber.Text;

        labelName.Text = name;
        labelNumber.Text = number;

You have a few options.. You can:

1- Send the textbox values to your B form like BForm B = new BForm(textBoxName.text,textBoxNumber.text)

2- Have a public property on FormA which gets the values of the textboxes so you can use them on FormB

This will not work, as the instance of FormA you are creating is not the original Instance that called/created FormB.

In form A you need to either make the textbox modifiers public, to be able to access them, or have public methods, that allows you to access the private textboxes.

Then also, when creating form b from form a, you will have to pass a reference of form a to form b.

The easiest way would be for formA to pass the values of the textboxes to form b.

So, where you create formb, do something like

FormB b = new FormB();
b.StringValue1 = pci.textBoxName.Text;
b.Stringvalue2 =  pci.textBoxNumber.Text;
b.Show();

Take a look around MVC design pattern, maybe your way is just a not really good way of doing it ;)

http://c2.com/cgi/wiki?ModelViewController

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM