委托案例_窗体传值

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 委托案例_窗体传值
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2(SetLabel);
            frm2.Show();        
        }
        //功能是将form2中的输入框内的值传给form1的label
        private void SetLabel(string s)
        {
            label1.Text = s;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = "";
            button1.Text = "Open Form2";
        }
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 委托案例_窗体传值
{
    public delegate void DelTest(string s);

    public partial class Form2 : Form
    {
        public DelTest _del;

        public Form2(DelTest del)
        {
            this._del = del;
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _del(textBox1.Text);
            
        }
        
        private void Form2_Load(object sender, EventArgs e)
        {
            button1.Text = "传值";
            textBox1.Text = "这是form2的内容~";
        }
    }
}
原文地址:https://www.cnblogs.com/Jason1019/p/8512365.html