Pass Form Values in C#: Transfer Data Between Forms

When developing an application in C#, it is common to have multiple forms that need to communicate and transfer data between each other. One way to achieve this is by passing form values between the forms.

The first step is to create a public property or method in the receiving form that will be used to receive the data from the sending form. For example, if we want to pass a string value from Form1 to Form2, we can create a public property in Form2 like this:

public string MyValue { get; set; }

Next, we need to instantiate Form2 from Form1 and pass the value using the public property we just created. We can do this by creating an instance of Form2 and setting the value of the public property before showing the form. Here is an example:

// Instantiate Form2
Form2 form2 = new Form2();

// Set the value of the public property
form2.MyValue = "Hello World!";

// Show Form2

Finally, in Form2, we can access the value of the public property and use it as needed. For example, we can display the value in a label control like this:

// Access the value of the public property
string myValue = this.MyValue;

// Display the value in a label control
label1.Text = myValue;

By using this technique, we can easily pass form values between forms in C# and ensure that our application is communicating and sharing data as needed.

Click to rate this post!
[Total: 0 Average: 0]

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Go up

Below we inform you of the use we make of the data we collect while browsing our pages. You can change your preferences at any time by accessing the link to the Privacy Area that you will find at the bottom of our main page. More Information