C# 从先前的表单获取文本框值
我有两种形式。
表单 A 要求用户在两个文本框中输入文本:姓名和号码。
表单 B 从表单 A 的两个文本框中获取文本,并将文本显示到两个标签中。
现在,当程序运行时,两个标签不显示文本。
请帮忙,提前致谢。
以下是我的表格 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;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看看 MVC 设计模式,也许你的方法并不是很好的方法;)
http: //c2.com/cgi/wiki?ModelViewController
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
您有几个选择..您可以:
1- 将文本框值发送到 B 表单,例如
BForm B = new BForm(textBoxName.text,textBoxNumber.text)
2- 在 FormA 上拥有公共属性它获取文本框的值,以便您可以在 FormB 上使用它们
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
这将不起作用,因为您正在创建的 FormA 实例不是调用/创建 FormB 的原始实例。
在表单 A 中,您需要将文本框修饰符设为公共,以便能够访问它们,或者具有公共方法,以允许您访问私有文本框。
然后,当从表单 a 创建表单 b 时,您必须将表单 a 的引用传递给表单 b。
最简单的方法是 formA 将文本框的值传递给 form b。
所以,在你创建表单的地方,做类似的事情
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