开发者

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;
        labelNumb开发者_StackOverflow社区er.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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜