Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To display multiple forms within a single panel in C# .NET4.8.1 using Visual Studio 2022, you can follow these steps:

  1. Create a new Windows Forms application project in Visual Studio 2022.
  2. Add a new UserControl to the project by right-clicking on the project and selecting Add > UserControl.
  3. Design the UserControl by dragging and dropping the desired controls onto it.
  4. Add a new form to the project by right-clicking on the project and selecting Add > Windows Form.
  5. Add a Panel control to the new form by dragging and dropping it from the Toolbox.
  6. Set the Dock property of the Panel control to Fill so that it takes up the entire form.
  7. In the form's code-behind file, add a function to show a UserControl within the Panel control:
private void ShowUserControl(UserControl userControl)
{
    panel1.Controls.Clear();
    panel1.Controls.Add(userControl);
}
  1. In the form's button click event handler, create an instance of the UserControl and call the ShowUserControl function:
private void button1_Click(object sender, EventArgs e)
{
    var userControl1 = new UserControl1(); // Replace UserControl1 with the name of your UserControl
    ShowUserControl(userControl1);
}
  1. Repeat step 8 for each UserControl you want to display within the Panel control.

Note: The above steps show how to display UserControls within a Panel control on a form. To display multiple forms within a Panel control, you can follow a similar approach, but instead of UserControls, you would create instances of your forms and set their TopLevel and FormBorderStyle properties to false before adding them to the Panel control.