How to: Get a Value from Another Form
Updated: September 2008
This topic describes how to retrieve a value from a text box on a Windows Form and display it in a text box on another form.
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.
To get a value from another form
On the File menu, point to New and then click Project.
In the New Project dialog box, in the Templates pane, click Windows Forms Application, and then click OK.
A new Windows Forms project opens.
In Solution Explorer, right-click the project, click Add, and then click New Item.
In the Add New Item dialog box, in the Templates pane, click Windows Form, and then click Add.
A new Windows Form (Form2) opens.
From the Toolbox, drag one TextBox control onto the form.
Change the Text property of the TextBox control to Sample text. The first form will read and display this property.
Change the Modifiers property of the TextBox control from Private to Public to enable other forms to access this control.
In Solution Explorer, right-click Form1.cs, and then click View Designer.
In the form, double-click the button to create a Click event handler.
Before the button1_Click event handler, add the following code to create an object that enables access to the components and properties of Form2:
In the button1_Click event handler, add the following code to get the value from the text box on Form2 and assign it to the text box on Form1:
Press F5 to run the program.
The program starts and the form appears. When you click the button on Form1, the text from the text box on Form2 appears in the text box on Form1.