C # WinForms - How to get data from a textbox on one form through another form?
You can open a property in one form and call it from another. Of course, you need to get an instance of form1 somehow. You can store it as a static property in the program class or another parent class. Usually in this case, I have a static application class that contains an instance.
public static class Application
{
public static MyForm MyFormInstance { get; set; }
}
Then, when you run the first form, set the application's MyFormInstance property to an instance of the first form.
MyForm instance = new MyForm();
Application.MyFormInstance = instance;
Add the property to the second form.
public String MyText
{ get { return textbox1.Text; }
set { textbox1.Text = value; }
}
And then you can access it from your second form with:
Application.MyFormInstance.MyText
a source to share
There is a similar post here
The videos below will clarify many of your concepts about transferring data between two forms.
There are several ways to pass data between the two forms that check these links, which have video examples for this.
-
FormToForm Using Properties - http://windowsclient.net/learn/video.aspx?v=108089
-
FormToForm Using Parameters - http://windowsclient.net/learn/video.aspx?v=105861
NTN
a source to share
Do not do that.
Longer version: Why does your view interact directly with another view?
Longer version:
Rather than creating a public property that exposes the field, it would provide better encapsulation and isolation from modification if the form with the field of interest interacted with some kind of data object that was then passed to the interested method.
The location of the method concerned must be carefully considered - if it controls aspects of the view (WinForm, in your case), then it might be a relevant member of that class - if not, perhaps its real home is in the data object?
a source to share