WPF 子窗体访问父窗体的函数

来源:互联网 发布:宜搜软件下载 编辑:程序博客网 时间:2024/05/20 03:44
http://www.silverlightchina.net/html/study/WPF/2011/0706/8827.html
子窗体代码 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
  

  子窗体代码

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://hi.baidu.com/lilipangtou/blog/item/92e091f89cb6153b4f4aeabd.html

0 0
原创粉丝点击