How to work with multiple pages or forms

31,321

Solution 1

One way is to create another form and open it from a button event:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2();
        form2.Show();
    }
}

If you want everything in one window, you can create a user control and add it to the first window:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        UserControl1 control = new UserControl1();
        control.Dock = DockStyle.Fill;
        this.Controls.Add(control);
    }
}

Another option is using child forms:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.IsMdiContainer = true;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2();
        form2.MdiParent = this;
        form2.Show();
    }
}

Solution 2

Create a panel for each button you have. Then:

panelx.Dock = DockStyle.Fill; //this will fill the window.

And put all you want to show for that button on that panel.

When you want to show, say panel2 instead of panel1:

panel1.Hide();
panel2.Show();

Solution 3

As mentioned earlier, this can be achieved using a tab control.

      public ParentForm()
    {
        TabControl tabcontrol1 = new TabControl();
        tabcontrol1.Dock = DockStyle.Top;
        TabPage tab1 = new TabPage("Form1 Name");
        Form1 frm1 = new Form1();
        frm1.TopLevel = false;
        frm1.Parent = tab1;
        frm1.Visible = true;
        tabcontrol1.TabPages.Add(tab1);

        Form2 frm2 = new Form2();
        TabPage tab2 = new TabPage("Form2 Name");
        frm2.TopLevel = false;
        frm2.Parent = discotab;
        frm2.Visible = true;
        tabcontrol1.TabPages.Add(discotab);
    }

Using "stacked" custom user controls

    public ParentForm()
    {
        InitializeComponent();
        MyUserControl1 control1 = new MyUserControl1();
        control1.Dock = DockStyle.Bottom;
        control1.BringToFront();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        MyUserControl2 control2 = new MyUserControl2();
        control2.Dock = DockStyle.Bottom;
        control2.BringToFront();
    }
Share:
31,321
TMan
Author by

TMan

Updated on July 10, 2020

Comments

  • TMan
    TMan almost 4 years

    I'm trying to create one window and on that window I would have a toolbar with different buttons.

    When I go to click on one of the buttons It would display something like information about a person or when I click on another button It would display some other information about employees.

    How can I do this. Can I make add pages and then insert that page onto a grid or panel when that button calls for it?

    Or Should I just make multiple panels and create them all on one window(but if I do this how would it be easy for me to edit each of those panels when they are stacked on one another all in one window). I hope I'm being clear about this, Idk how else to ask this question. Any help is appreciated.

    Also how do I dock something so that it resizes itself when maximize or minimize?