极品分享

C# 两个窗口见获取事件获取—委托与订阅

 子窗体代码

namespace WindowsApplication1 {    //定义委托    public delegate void ChangeTextHandler(string text);    public partial class ChildFrm : Form     {        //定义事件        public event ChangeTextHandler ChangeTextEvent;        public ChildFrm() {            InitializeComponent();        }        /// <summary>        ///         /// </summary>        /// <param name="sender">事件源</param>        /// <param name="e">事件对象</param>        private void radioButton1_CheckedChanged(object sender, EventArgs e) {                        RadioButton rdo = sender as RadioButton;            //引发事件            if (ChangeTextEvent != null) {                ChangeTextEvent(rdo.Text);            }        }    }}父窗体代码namespace WindowsApplication1 {    public partial class Form1 : Form {        public Form1() {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e) {            ChildFrm frm = new ChildFrm();            //订阅事件            frm.ChangeTextEvent += new ChangeTextHandler(frm_ChangeTextEvent);            frm.ShowDialog();        }        //根据订阅的事件进行执行        void frm_ChangeTextEvent(string text) {            this.textBox1.Text = text;        }            }}

 

2013-04-24 0 /
NET学习
/
标签: 

评论回复

回到顶部