Access to the parent form

I know that the name may seem silly, could not come up with something better, sorry.

I have 2 forms (C #), the main form contains an instance of the second. Is there a way ... to access an executable instance of Form1 (entry point) and its properties from an instance of form2?

Everyone tells me to study OOP. I did this a long time ago, and I still don't get it.

+3
source share
2 answers

You probably created Form2 from Form1. After starting and before showing, you can set the Form2 property, which refers to Form1 as follows:

Form2 f2 = new Form2();
f2.TheParent = this;
f2.Show();

Of course, you need to add the TheParent property to the Form2 class to do this.

: , , / . .

+4

, .

, .

Form1 Form2 - Form1 ctor:

Form2 f2 = new Form2(this);

Form2 :

private Form1 m_form = null;

:

public Form2(Form1 f)
{
   m_form = f;
}

, Form2, Form1 m_form

+8

Source: https://habr.com/ru/post/1793208/


All Articles