C# WinFrom 窗体传值

   public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public string TextBox2Value //将外部不可访问的textBox2.Text封装成属性TextBox1Value
        {
            set { textBox2.Text = value; }
            get { return textBox2.Text; }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;//这里的DialogResult是Form2类对象的属性
        }

    }

委托传值

using System;
using System.Windows.Forms;

namespace WindowsForms跨窗体传值大全
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.ChangeText += new ChangeTextHandler(Change_Text);//将事件和处理方法绑在一起,这句话必须放在f2.ShowDialog();前面
            f2.ShowDialog();            
        }

       public void Change_Text(string str)
        {
            textBox1.Text = str;
        }  
    }
}   



using System;
using System.Windows.Forms;

namespace WindowsForms跨窗体传值大全
{
    public delegate void ChangeTextHandler(string str);  //定义委托

    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public event ChangeTextHandler ChangeText;  //定义事件

        private void button2_Click(object sender, EventArgs e)
        {
            if (ChangeText != null)//判断事件是否为空
            {
                ChangeText(textBox2.Text);//执行委托实例  
                this.Close();
            }           
        }    
    }
}

Action<>泛型委托和lambda表达式

//Form1中:
using System;
using System.Windows.Forms;

namespace WindowsForms跨窗体传值大全
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.ChangeText = (str) => textBox1.Text = str;//用lambda表达式实现,这句话必须放在f2.ShowDialog();前面
            f2.ShowDialog();            
        }
    }
}


//Form2中:
using System;
using System.Windows.Forms;

namespace WindowsForms跨窗体传值大全
{  
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public Action<string> ChangeText;//之前的定义委托和定义事件由这一句话代替

        private void button2_Click(object sender, EventArgs e)
        {
            if (ChangeText != null)//判断事件是否为空
            {
                ChangeText(textBox2.Text);//执行委托实例  
                this.Close();
            }           
        }    
    }
}
原文地址:https://www.cnblogs.com/lanyubaicl/p/11021564.html