Search code examples
c#staticmdichildmdiparent

Passing integer value from MDI parent to child and back


I have a Windows Forms app with an MDI parent form (mdiBK) and a few child forms (form1 and form2 are the ones that matter).

I would like to set an static int variable inside my parent form (mdiBK) and set it to '0'.

On 'form1' there are 2 buttons and both close form1 and open form2 I want to set the static variable (in mdiBK) to 1 when button1 is clicked and set it to 2 when button2 is clicked.

In form2 there is a button that closes form2 and returns control to the parent form (mdiBK) and I want to set my variable back to zero.

What code should I write to do that?


Solution

  • The proper way of exposing content in OOP is to use properties.
    you can add a static property on your mdi parent form and use it anywhere in the code:

    on the MDI form:

    public static int MyInt {get;set;}
    

    inside the button1 click event handler:

    mdiBK.MyInt = 1;
    

    inside the button2 click event handler:

    mdiBK.MyInt = 2;
    

    on the second form, inside the button click event handler:

    mdiBK.MyInt = 0;