1.示例代码,假如主窗体为MainForm,有三个子窗体分别是:Form1,Form2,Form3

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
    }
    Form1 form1 = new Form1(); //子窗体1
    Form2 form2 = new Form2(); //子窗体2
    Form3 form3 = new Form3(); //子窗体3
    private void MainForm_Load(object sender, EventArgs e) //主窗体加载事件
    {
        ShowForm(form3);  //将窗体加载至panle控件中
        ShowForm(form2);
        ShowForm(form1);
    }
    private void ShowForm(Form frm) //显示子窗体
    {
        if (!splitContainer1.Panel2.Controls.Contains(frm))
        {
            frm.TopLevel = false; //窗体是否为顶级窗体
            frm.FormBorderStyle = FormBorderStyle.None; //设置窗体无边框
                                                        // frm.Dock = DockStyle.Fill;     //设置填充方式为充满
            frm.Parent = this.splitContainer1.Panel2; //设置父窗体  随业务逻辑变动
            splitContainer1.Panel2.Controls.Add(frm);
            frm.Show();                    //打开窗体
            frm.BringToFront(); //将控件显示最前
        }
        else
        {
            frm.BringToFront();
        }
    }

    private void btnShowForm1_Click(object sender, EventArgs e) //切换子窗体1
    {
        ShowForm(form1);
        BackColorSet(((System.Windows.Forms.Button)sender).Text);
    }
    private void btnShowForm2_Click(object sender, EventArgs e) //切换子窗体2
    {
        ShowForm(form2);
        BackColorSet(((System.Windows.Forms.Button)sender).Text);
    }
    private void btnShowForm3_Click(object sender, EventArgs e) //切换子窗体3
    {
        ShowForm(form3);
        BackColorSet(((System.Windows.Forms.Button)sender).Text);
    }
    private void BackColorSet(string item)  //设置按钮背景色
    {
        //①现将所有的按钮统一赋值为同一种颜色
        this.btnShowForm1.BackColor = Color.Teal;
        this.btnShowForm2.BackColor = Color.Teal;
        this.btnShowForm3.BackColor = Color.Teal;
      
        //②根据传入的参数判断当前是哪个按钮按下
        switch (item)
        {
            case "显示子窗体1":
                this.btnShowForm1.BackColor = Color.Red;
                break;
            case "显示子窗体2":
                this.btnShowForm2.BackColor = Color.Red;
                break;
            case "显示子窗体3":
                this.btnShowForm3.BackColor = Color.Red;
                break;
           
            default:
                break;
        }
    }
}

运行结果

C# 主窗体中显示子窗体(MDI)-LMLPHP

03-25 21:17