WinForm子窗体访问父窗体控件的实现方法
程序员文章站
2023-12-14 18:13:40
form1主窗体:
复制代码 代码如下:public delegate void setvisiablehandler();//定义委托类型
private void...
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();
}
}