This tutorial allows you to retrieve a variable from one form and display it in a textbox on another form. So you will learn in this tutorial how to pass data from one form to another in C#
Create Visual Studio Form project, then add form2 into project. After adding forms, add those components into forms.
That’s what you need for both forms:
Click button1 in the form1 and add this code. We will change the constructor method in a parameterized way.
1 2 3 4 5 6 7 |
private void button1_Click(object sender, EventArgs e) { Form Form2 = new Form2(textBox1.Text); Form2.ShowDialog(); } |
Change the Form2 consturator as this:
1 2 3 4 5 6 7 8 9 10 11 12 |
public partial class Form2 : Form { /*add string value to Form2 constructor*/ public Form2(string value) { InitializeComponent(); textBox1.Text = value; label1.Text = value; } ...... |