子窗体调用父窗体(转)

///////////////////////////////////////////////////////////////////////
///// 父窗体,你需要添加一个button(并关联button1_Click)和一个textbox
///////////////////////////////////////////////////////////////////////
using System;
using System.Windows.Forms;

namespace Demo
{
public partial class ParentForm : Form
{
ChildForm childForm;
public ParentForm()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
childForm= new ChildForm();
childForm.DataArrivalEvent+=new ChildForm.DataArrivalEventHandler(childForm_DataArrivalEvent);
//显示子窗体
childForm.ShowDialog();
}

//事件处理
void childForm_DataArrivalEvent(string msg)
{
textBox1.Text = msg;
}
}
}
///////////////////////////////////////////////////////////////////////
///// 子窗体 你需要添加一个button,并关联button1_Click
///////////////////////////////////////////////////////////////////////
using System;
using System.Windows.Forms;

namespace Demo
{
public partial class ChildForm : Form
{
//接收信息事件委托
public delegate void DataArrivalEventHandler(string msg);
//事件对象
public event DataArrivalEventHandler DataArrivalEvent;

static int clickCount = 0;

public ChildForm()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
//如果父窗体已注册了自定义事件
if (DataArrivalEvent != null)
{
DataArrivalEvent(String.Format("单击:{0}次", clickCount++));
}
}
}
}
posted @ 2013-11-25 14:28  苗苗她爹  阅读(190)  评论(0编辑  收藏  举报