C# 在子窗体调用父窗体的值(转)

1、在父窗体中

public delegate void SetVisiableHandler();//定义委托类型 注意此委托定义在namespace下一级,在form类外

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");
}

2、子窗体中

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();
    }
}

 

posted on 2016-03-28 22:49  jin_qi_er  阅读(1254)  评论(0编辑  收藏  举报