AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > .NET技术

C#中自定义事件的处理

51自学网 http://www.wanshiok.com

原帖及讨论:http://bbs.bccn.net/thread-106377-1-1.html

/// form1为基类
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        //我自定义的事件
        public event EventHandler  myevent;
      
        public Form1()
        {
            InitializeComponent();
        }
        public  void myfunc(int a, int b)
        {
            if (myevent != null)
            {
                myevent(null , null);
            }
        }
    }
}

/// form2从form1派生,form2为执行类
namespace WindowsApplication1
{
    public partial class Form2 : Form1
    {
        Form1 f = new Form1();

        public Form2()
        {
            InitializeComponent();
            f.myevent += new System.EventHandler(diaoyong);
        }    

        public void diaoyong(object o,EventArgs e)
        {

            MessageBox.Show ("派生类执行");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            f.myfunc(1, 1);
        }
        
    }
}
运行结果是 : 弹出对话框"派生类执行";
public Form2()
        {
            InitializeComponent();
            f.myevent += new System.EventHandler(diaoyong);//这里是注册
        }  


myevent(null , null);//这是执行,调用的就是所注册的 diaoyong()

 

 

 
上一篇:[图文]手把手教你用C#打包应用程序(安装程序)【卸载模块已添加】  下一篇:C#简明教程