天天看點

WPF子視窗關閉時更新父視窗資料

子窗體代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
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);
            }
        }
        private void ChildFrm_MouseClick(object sender, MouseEventArgs e) {
            //e.Button == MouseButtons.Right
        }
        private void ChildFrm_Load(object sender, EventArgs e)
        {
        }
    }
}
父窗體代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
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;
        }
            }
}
本文轉自:http://s.yanghao.org/program/viewdetail.php?i=412042      

繼續閱讀