WinForm子窗体访问父窗体控件的实现方法
发布时间:2016-12-28 来源:查字典编辑
摘要:Form1主窗体:复制代码代码如下:publicdelegatevoidSetVisiableHandler();//定义委托类型priva...
Form1主窗体:
复制代码 代码如下:
public delegate void SetVisiableHandler();//定义委托类型
private void button1_Click(object sender,EventArgs e)//单击事件,打开子窗体
{
Form2 frm = new Form2(new SetVisiableHandler(SetVisiable));
frm.Show();
}
private void SetVisiable()
{
dataGridView1.Rows.Add("123","456","789");
}
Form2子窗体:
复制代码 代码如下:
private SetVisiableHandler m_setVisable;//声明委托变量
public Form2(SetVisiableHandler setvisiable)
{
InitializeComponent();
this.m_setVisiable = setvisiable;
}
public void butten1_Click(object sender,EventArgs e)
{
if(this.m_setVisiable != null)
{
this.m_setVisiable();
}
}