且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从子窗体访问主窗体

更新时间:2023-08-15 20:39:16

方案1:单击子窗体中的按钮,然后在父窗体"中调用方法.

Scenario 1: Call a method in Parent Form on click of button in child form.

在子窗体中创建一个 Event .在某些按钮单击"上引发该事件.在父表单"中订阅该事件并在其中调用父表单的方法.

Create an Event in Child Form. Raise that event on some Button Click etc. Subscribe to that event in your Parent Form and call the parent's form method inside that.

方案2:关闭子窗体后,在父窗体中调用方法.

Scenario 2: Call a method in Parent Form when Child Form is closed.

在父表单中处理子表单的 FormClosed FormClosing 事件,并在其中调用父表单的表单方法.

Handle the FormClosed or FormClosing event of Child Form in the Parent form and call the parent's form method inside that.

ChildForm frm = new ChildForm();
frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);

void frm_FormClosed(object sender, FormClosedEventArgs e)
    {
        //Call your method here.
    }