Question

I am working in Desktop application in C#. I have an MDI Parent form having Panel control for adding Child Forms in it. I have 2 childs forms:

  1. ChildForm1
  2. ChildForm2

Childform2 is opened from childform1.

From MDI form i add childform1 to panel control, know i want to add childform2 also to mdi Form panel control. When i click button on childform1, from mdi form panel control childform1 must be removed and childform2 will be added to mdi form panel control.

How to solve this problem?

Was it helpful?

Solution

You first open ChildForm2 from ChildForm1 by passing refrence of ChildForm1 to ChildForm2

 ChildForm2 objChildForm2 = new ChildForm2(this);
 objChildForm2.MdiParent = this.MdiParent;
 objChildForm2.Show();

Now Go to your ChildForm2 and Add Parametrize contructor also create Global object of ChildForm1

ChildForm1 objChildForm1 = null;

 public ChildForm2(ChildForm1 obj)
    {
        InitializeComponent();

        objChildForm1 = obj; // Passing refrence of ChildForm1
        obj.Hide(); // this will hide the ChildForm1
    }   

Now when your ChildForm2 open ,the obove parametrize constructor will call and it will hide your ChildForm1. Also you can reopen your ChildForm1 on close of ChildForm2.Write following code for that.

    private void btnClose_Click(object sender, EventArgs e)
    {

        objChildForm1.Show();// you can access all public members of ChildForm1 by the Global   //object "objChildForm1"
        this.Close();
    } 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top