SoFunction
Updated on 2025-03-07

A brief analysis of various methods of passing values ​​between Winform windows in C#

summary

General projects are composed of multiple forms, and data must be flexibly transmitted between each form. Let’s share some of your experiences:

There are many ways to pass values ​​in the form. The following is only a few of them I have used. I don’t know what the official method is called. You can also look for others.

Passing values ​​through the constructor

This is the easiest way, for example, I want to pass a string from form1 to form2
First, make a slight modification in the form2 constructor:

Copy the codeThe code is as follows:

        public Form2(String s)
        {
            InitializeComponent();
            = s;
    }

Added a string parameter s
Then, in the click event of form1, the parameters to be passed are passed in, as follows:
Copy the codeThe code is as follows:

private void buttonShow_Click(object sender, EventArgs e)
        {
            …
            Form2 f2;
            if ( == "")
            {
                f2 = new Form2("I'm from form1");
            }
            else
            {
                f2 = new Form2();
              }
DialogResult r = ();//As for Show and ShowDialog, there is no need to talk about it
               …
           }

When the click event is triggered, form2 accepts the "I'm from form1" transmitted by f1 and uses labelRecieve to receive it.
Although this method is simple and convenient, it has limited functions after all. When it is necessary to pass a large amount of diverse data, it is not competent with the constructor.

Passing through attributes

Attributes can easily pass multiple data (are you just adding multiple attributes?). Suppose I want to pass a string from form2 to form3, first we add a property called myString for form3.

Copy the codeThe code is as follows:

 public Form3()
        {
            InitializeComponent();
        }
        public string myString
        {
            get
            {
                return this.;
            }
            set
            {
                if (value == null)
                    = ;
                else
                    = value;
            }
         }

Use this property to set the text box text.
Then, in the form2 click event, just write

Copy the codeThe code is as follows:

      = "I'm from form2";

This is also very convenient. It is worth mentioning that when passing through attributes, data can be transmitted from form2 to form3 and back from form3. like
Copy the codeThe code is as follows:

   if (r == )
            {
                = "I'm from form3";
                = ;
//Save myString to form2 label when form3 is closed.
             }

Property transfer is the most commonly used method.

Passing parameters through event carrying

To be honest, I rarely use this at the moment. To be precise, this should be called event combination attribute transmission.
Now I have some operations in form4. Assuming that form4 is part of the type library we developed, I want to publish it to other colleagues for use, we can use such events to pass values.
First, in form4, add the definition of event parameter type and event handler delegate:

Copy the codeThe code is as follows:

 public class myEventArgs : EventArgs
        {
            string myString;
            public string MyString
            {
                get
                {
                    return myString;
                }
                set
                {
                    if (value != null)
                        myString = value;
                    else
                        myString = ;
                }
              }

            public myEventArgs(string s)
            {
               = s;
            }
        }  

//Delegation of event handling method
        public delegate void myEventHandler(object sender, myEventArgs e);

Then, we trigger such a public event in the click event myClick

Copy the codeThe code is as follows:

 public event myEventHandler myClick;
          private void buttonClick_Click(object sender, EventArgs e)
      {
            if ( != null)
                (this,new myEventArgs());
            = ;
    }

We encapsulate the text in textBox4 into an event parameter object like myEventArgs, so that it will be passed to form3 as the event is passed. At the same time, we will add an event handling method in form3.

Copy the codeThe code is as follows:

        void f4_myClick(object sender, e)
        {
            myString = ;
            this. = ("From f4: {0}",);
        }  

In the click event, we call the method by delegating it

Copy the codeThe code is as follows:

   Form4 f4 = new Form4();
   += new (f4_myClick);

After the myClick event of the f4 object is triggered, the f4_myClick method here will be called, and we can obtain data from the event parameters through this method. This is put into textBoxf3.

Let’s just write this. The article describes the most basic transfers. Of course, the parameters passed can be of various types, or generic delegation can be used, or the Control in a FORM can be "passed" into another Form. Everyone can do it themselves :)