windows 如何使用文本框的值从一种形式到另一种形式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1818721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use value of textbox from one form to another form?
提问by ush
Can we access a textbox value of one form in another form?
我们可以以另一种形式访问一种形式的文本框值吗?
回答by Joey
You can make the text box public for that form. To do this, change the access modifier property in the properties of the text box:
您可以将该表单的文本框设为公开。为此,请更改文本框属性中的访问修饰符属性:
Or you can create a public property that exposes the textbox's value:
或者您可以创建一个公开文本框值的公共属性:
public string Foo {
get { return txtFoo.Text; }
}
The latter is probably preferrable if you only need read-only access to the textbox's text. You can add a setter as well if you also need to write it. Making the complete textbox public allows for much more access than you probably want to have in this instance.
如果您只需要对文本框的文本进行只读访问,则后者可能更可取。如果您还需要编写它,也可以添加一个 setter。在这种情况下,将完整的文本框设为公开可以提供比您可能想要的更多的访问权限。
回答by Fernando
Another way is pass the TextBox
to the constructor of the other form, like this:
另一种方法是将 传递TextBox
给另一种形式的构造函数,如下所示:
private TextBox _control;
public SomeForm(TextBox control)
{
InitializeComponent();
this._control = control;
}
and use the
并使用
this._control.text = "bla bla";