lhunter664
lhunter664

Reputation: 99

How to open form from Child Form and show in main form panel

I am currently developing a Windows Form application in C# Visual Studio 2017 and decided to implement a side panel menu using the tutorial seen here.

It is great for displaying the child forms when clicked, however I was wondering how I can replace the current child form with a new child form, within the child form code. I.e. how do I access the main form within the child form code.

private Form activeForm = null;
    private void openChildForm(Form childForm)
    {
        if (activeForm != null)
            activeForm.Close();
        activeForm = childForm;
        childForm.TopLevel = false;
        childForm.FormBorderStyle = FormBorderStyle.None;
        childForm.Dock = DockStyle.Fill;
        childFormPanel.Controls.Add(childForm);
        childFormPanel.Tag = childForm;
        childForm.BringToFront();
        childForm.Show();
    }

That is how the child form is opened within the main form. How can I access this method through the child form? I trust this may be quite confusing but if you have a quick look at the video, you'll understand what I mean.

Thanks!

Upvotes: 2

Views: 2892

Answers (2)

bPuhnk
bPuhnk

Reputation: 385

It is possible to use a static class that retains an instance of your main form. When you main form loads set the MainForm static property in the FormController class. Then from your child forms call the FormControll.openChildForm(...);

public static FormController{
    public static Form MainForm {get;set;}
    
    private static Form activeForm = null;
    public static void openChildForm(Form childForm)
    {
       if (activeForm != null)
          activeForm.Close();
       activeForm = childForm;
       MainForm.childForm.TopLevel = false;
       MainForm.childForm.FormBorderStyle = FormBorderStyle.None;
       MainForm.childForm.Dock = DockStyle.Fill;
       MainForm.childFormPanel.Controls.Add(childForm);
       MainForm.childFormPanel.Tag = childForm;
       MainForm.childForm.BringToFront();
       MainForm.childForm.Show();
     }
}

Then in your child forms you can call that static class to change forms.

public class ChildForm{
    private void btn_click(...){
        FormController.openChildForm(<newChildForm>);
    }
}

Upvotes: 2

Jay Mason
Jay Mason

Reputation: 470

You can use events to handle this:

Step 1) Create a new class that inherits from Form. This will be your base class for all of your children's forms. You can call this ChildForm (inherits from) Form.

Step 2) Define a new public Event that you can bind to from your function of changing the child form.

That could look something like this:

public event EventHandler OnRequestChildChange;

Step 3) In your child where you want to request a change you can call to this:

OnRequestChildChange?.Invoke(null); //This would pass in your parameters as well, so you could create an event that allows you to pass in custom parameters and pass in the name or type of form, and have a switch statement in your event handler.

Custom Events: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-publish-events-that-conform-to-net-framework-guidelines

Step 4) Bind to the event in your main function

private ChildForm activeForm = null;
private void openChildForm(ChildForm childForm)
{
  if (activeForm != null){
    activeForm.OnRequestChildChange -= Child_RequestChange;
    activeForm.Close();
  }

  activeForm = childForm;
  activeForm.OnRequestChildChange += Child_RequestChange;
  childForm.TopLevel = false;
  childForm.FormBorderStyle = FormBorderStyle.None;
  childForm.Dock = DockStyle.Fill;
  childFormPanel.Controls.Add(childForm);
  childFormPanel.Tag = childForm;
  childForm.BringToFront();
  childForm.Show();
}

Step 5) Create the event handler 'Child_RequestChange' which will handle swapping the forms when the child requests by creating a new instance, or pulling a local instance of the new form you want and calling the method above.

Upvotes: 0

Related Questions