INTRODUCTION
This is my first mini tutorial, and it’s just a passing of variable between two forms. This allows you to retrieve a variable from one form and display it in a textbox of another form. So you will learn in this tutorial How to Pass Textbox value from one Form to another Form in C#.
TUTORIAL
Create the first form, add a textBox1 and a button, Enter a text in the text Box1 and click on the button to display the value of the textBox1 in another textBox2 of the second form.
That’s what you need for both forms:
For the form1:
1 2 3 4 5 6 7 | private void button1_Click(object sender, EventArgs e) { Form Form2 = new Form2(textBox1.Text); Form2.ShowDialog(); } |
For the form2: 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; } ...... |