简体   繁体   English

如何使用文本框的值从一种形式到另一种形式?

[英]How to use value of textbox from one form to another form?

我们可以访问另一种形式的文本框值吗?

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. 如果还需要写一个setter,也可以添加它。 Making the complete textbox public allows for much more access than you probably want to have in this instance. 将整个文本框设为公开可以提供比您在这种情况下可能想要的更多的访问。

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";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM