C#’s ability to switch between subforms in the main form relies on the panel container and Controls functions.
Hello, everyone! I’m grey Ape!
Today to share with you in C# winform development how to achieve the use of a main form to achieve internal multiple child form switching effect.
Let’s first look at the effect of multiple small forms in the main form:
The principle of multi-form switching: the principle of multi-form switching is actually using a panel container, in this container to display the same size of the window,
Next, the big bad Wolf will share with you the steps to create a multi-window switch:
1. Create a new main form and place the appropriate controls in it, including a button to switch to and a Panel container to display the form. Note that the child form will be displayed in this Panel container.
The effect is as follows:
2, create a new child form for display in the main form. The new child form is actually a user control (Windows Form). Here we can right-click in the status bar and choose – > Add – > User control (Windows Form).
3. Set the size of the child form to be equal to the size of the Panel container so that it will not overflow when displayed in the Panel container. Here we set the color of the form to make it easier to distinguish between the three different child forms and prompt it in the form.
Create child Forms 2 and 3 in the same way.
The form of three
5. Define the corresponding three window variables in the main form’s code,
public UserControl1 f1; // Create a user control variable
public UserControl2 f2; // Create user control two variables
public UserControl3 f3; // Create three variables for the user control
Copy the code
Assign to three variables in the Form1_Load function, that is, instantiate three objects
private void Form1_Load(object sender, EventArgs e)
{
f1 = new UserControl1(); // instantiate f1
f2 = new UserControl2(); // instantiate f2
f3 = new UserControl3(); // instantiate f3
}
Copy the code
7. Since our form toggle is triggered by clicking the corresponding button, we need to set the trigger event for the corresponding button control. Double-click the button in the main form to enter the corresponding trigger function, here we will use the following code to display the corresponding child form:
// Displays form 1
private void button1_Click(object sender, EventArgs e)
{
f1.Show(); // Display form 1
panel2.Controls.Clear(); // Empty the controls on the original container
panel2.Controls.Add(f1); // Add form 1 to the container panel2
}
// Displays form 2
private void button2_Click(object sender, EventArgs e)
{
f2.Show(); // Display form 2
panel2.Controls.Clear(); // Empty the controls on the original container
panel2.Controls.Add(f2); // Add form 2 to the container panel2
}
// Displays form 3
private void button3_Click(object sender, EventArgs e)
{
f3.Show(); // Display form 3
panel2.Controls.Clear(); // Empty the controls on the original container
panel2.Controls.Add(f3); // Add form 3 to the container panel2
}
Copy the code
8. After completion, run the window and click the corresponding button to display the corresponding interface in the Panel2 container.
Finally, the complete source code is attached
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace window_cut
{
public partial class Form1 : Form
{
public UserControl1 f1; // Create a user control variable
public UserControl2 f2; // Create user control two variables
public UserControl3 f3; // Create three variables for the user control
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
f1 = new UserControl1(); // instantiate f1
f2 = new UserControl2(); // instantiate f2
f3 = new UserControl3(); // instantiate f3
}
// Displays form 1
private void button1_Click(object sender, EventArgs e)
{
f1.Show(); // Display form 1
panel2.Controls.Clear(); // Empty the controls on the original container
panel2.Controls.Add(f1); // Add form 1 to the container panel2
}
// Displays form 2
private void button2_Click(object sender, EventArgs e)
{
f2.Show(); // Display form 2
panel2.Controls.Clear(); // Empty the controls on the original container
panel2.Controls.Add(f2); // Add form 2 to the container panel2
}
// Displays form 3
private void button3_Click(object sender, EventArgs e)
{
f3.Show(); // Display form 3
panel2.Controls.Clear(); // Empty the controls on the original container
panel2.Controls.Add(f3); // Add form 3 to the container panel2}}}Copy the code
Feel good remember to like attention yo!
Big bad Wolf accompany you to progress together!