c#父子窗体间的自定义事件(static)



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

namespace ppop_client
{
    public partial class MDI : Form
    {
        private int childFormNumber = 0;

        public MDI()
        {
            InitializeComponent();
        }

        private void ShowNewForm(object sender, EventArgs e)
        {
            // 创建此子窗体的一个新实例。

            Form childForm = new Form1();
            // 在显示该窗体前使其成为此 MDI 窗体的子窗体。

            childForm.MdiParent = this;
            childForm.Text = "窗口" + childFormNumber++;

            childForm.Show();
        }
//根据委托定义事件
public static event meEventHandler me1;
//按钮引发事件
        private void toolStripButton_test_Click(object sender, EventArgs e)
        {
            if (me1 != null)
                me1(0);
        }
    }
}



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

namespace ppop_client
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

             //注册事件到本类的onme1函数
                MDI.me1+=new meEventHandler(onme1);
        }
            //事件处理
        public void onme1(object o)
        {
            label1.Text = "heheh";
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        
    }
}


using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace ppop_client
{

//定义一个委托
    public delegate void meEventHandler(object o);

    static class Program
    {
        /// <summary>

        /// 应用程序的主入口点。

        /// </summary>

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MDI());
        }
    }
}


作者: cexoyq   发布时间: 2011-01-05