C# 从先前的表单获取文本框值

发布于 2024-10-09 13:03:36 字数 440 浏览 0 评论 0原文

我有两种形式。

表单 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情绪失控 2024-10-16 13:03:37

看看 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

浅语花开 2024-10-16 13:03:36

您有几个选择..您可以:

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

A君 2024-10-16 13:03:36

这将不起作用,因为您正在创建的 FormA 实例不是调用/创建 FormB 的原始实例。

在表单 A 中,您需要将文本框修饰符设为公共,以便能够访问它们,或者具有公共方法,以允许您访问私有文本框。

然后,当从表单 a 创建表单 b 时,您必须将表单 a 的引用传递给表单 b。

最简单的方法是 formA 将文本框的值传递给 form b。

所以,在你创建表单的地方,做类似的事情

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文