athgap
athgap

Reputation: 165

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;

Upvotes: 0

Views: 2885

Answers (3)

ykatchou
ykatchou

Reputation: 3727

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

Upvotes: 0

Pabuc
Pabuc

Reputation: 5638

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

Upvotes: 2

Adriaan Stander
Adriaan Stander

Reputation: 166366

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();

Upvotes: 1

Related Questions