Passing value from one to another form

Introduction:

Here, I am creating a window form application and passing value from one form to another form. For doing this, set the "
Modifiers" property of control and assign value to it from another form.

Now, take a window form application. Here I am taking Form1 and arrange UI controls as given in below figure.



Add another form in your application and take a Label control on new form. Screen - shot is given below.



Now, go to "Modifiers" property of label and set it to "Public".



Write the following code on button click event of Form1.

private void btnok_Click(object sender, EventArgs e)
         {
             Form2 obj = new Form2();
             obj.label1.Text = "Hi, " + textBox1.Text;
             obj.Show();
         }

Now run the application.

Output:



Click the "ok" button. It will show the textbox value on Form2 from Form1.
 

Ebook Download
View all
Learn
View all