Access to the main form from the children's form

I have a simple problem: I have a main form in win-forms / C #. It has a list bound to a database.

When I click the button, a new form is created.

When I click on a button on a child form, I want to call a method that exists in the main form, which updates the list box or, alternatively, when the child form closes, to call this function.

Is it possible?

+5
source share
2 answers

Scenario 1. Call the method in the parent form by clicking the button in the child form.

Event . .. .

2. , .

FormClosed FormClosing .

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

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

, . , , :

ChildForm child = new ChildForm();
child.Show(this); // this calls the override that takes Owner parameter

, , (, MainForm):

MainForm parent = (MainForm)this.Owner;
parent.CallCustomMethod();

, (, , ) . , , ( , ).

+14

All Articles