C# 两个form之间传值、多播委托:委托可以指向多个函数

来源:互联网 发布:linux将文件夹压缩命令 编辑:程序博客网 时间:2024/06/05 14:58

实现了两个窗口的传值


Form1.csnamespace feiqiu{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            Form2 for1 = new Form2(ShowMsg );            for1.Show();        }        void ShowMsg(string st) //需要将这个方法传递到Form2中,使用委托在构造函数中实现        {            label1.Text =st;   //这样就可以在form2中有方法也有值,完成        }    }}
Form2



Form2.csnamespace feiqiu{    public delegate void DelTest(string str);    public partial class Form2 : Form    {        public DelTest _dt;        public Form2(DelTest del)        {            this._dt =del;            InitializeComponent();        }        private void label1_Click(object sender, EventArgs e)        {            //_dt(textBox1.Text );        }        private void button1_Click(object sender, EventArgs e)        {            _dt(textBox1.Text);        }    }}



委托可以实现多个方法同时实现

namespace delegation2{    public delegate void DelAdd1();        class Program    {        static void Main(string[] args)        {            DelAdd1 d1 = Show1;            d1 += Show2;            d1 += Show3;            d1 += Show4; //同时实现了Show1、Show2、Show3、Show4多个方法            d1 -= Show2;//同时实现了Show1、Show3、Show4多个方法            d1();            Console.ReadKey();        }        public static void Show1()        {            Console.WriteLine("The delegate One");        }        public static void Show2()        {            Console.WriteLine("The delegate Two");        }        public static void Show3()        {            Console.WriteLine("The delegate Three");        }        public static void Show4()        {            Console.WriteLine("The delegate Four");        }        public static void Show5()        {            Console.WriteLine("The delegate Five");        }    }}


原创粉丝点击