Reputation: 815
Can we access a textbox value of one form in another form?
Upvotes: 0
Views: 5187
Reputation: 354356
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.
Upvotes: 8
Reputation: 457
Another way is pass the TextBox
to the constructor of the other form, like this:
private TextBox _control;
public SomeForm(TextBox control)
{
InitializeComponent();
this._control = control;
}
and use the
this._control.text = "bla bla";
Upvotes: 0