代码改变世界

C#应用程序中的Delegate操作

2010-08-09 15:49  ※森林小居※  阅读(488)  评论(1编辑  收藏  举报

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 WinForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public delegate void ts();

        public static event ts onTs;

        private void button1_Click(object sender, EventArgs e)
        {
            onTs += new ts(Form1_onTs);
            Form2 f2 = new Form2();
            f2.Show();
        }

        void Form1_onTs()
        {
            label1.Text = "正在操作delegate";
        }

        public void Test() {
            onTs();
        }
    }
}

 

 

 

 

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 WinForm
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 f1 = new Form1();
            f1.Test();
        }
    }
}