C# 多窗口切换的实现

1、目的
在主窗口中根据不同的按钮选择不同的子窗口显示。
2、实现
(1)、创建Winform窗体程序,放入SplitContainer控件splitContainer1将窗体分成左右2部分;
(2)、在左侧splitContainer1.panel1中放入3个Button控件,button1(窗口1)、button2(窗口2)、button3(窗口3);
(3)、创建子窗体Form1,放置2个Label,Title和Content;
(4)、修改Form1的实现代码

public partial class Form1 : Form
    {
        public Form1(string title,string content)
        {
            InitializeComponent();
            lbTitle.Text = title;
            lbContent.Text = content;
        }
    }
(5)、在主窗体初始化3个Form1窗体
 public partial class MainForm : Form
    {
        public Form1 f1;
        public Form1 f2;
        public Form1 f3;
        public MainForm()
        {
            InitializeComponent();
            
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            string title = "窗口一";
            string content = "北国风光,千里冰封。";
            f1 = new Form1(title, content);
            f1.Dock = System.Windows.Forms.DockStyle.Fill;
            f1.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            f1.TopLevel = false;
            title = "窗口二";
            content = "成吉思汗,只识弯弓射大雕。";
            f2 = new Form1(title, content);
            f2.Dock = System.Windows.Forms.DockStyle.Fill;
            f2.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            f2.TopLevel = false;
            title = "窗口三";
            content = "俱往矣,数风流人物还看今朝。";
            f3 = new Form1(title, content);
            f3.Dock = System.Windows.Forms.DockStyle.Fill;
            f3.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            f3.TopLevel = false;
        }
    }

(6)、给3个Button控件添加Click处理函数

private void button1_Click(object sender, EventArgs e)
        {
            f1.Show();
            splitContainer1.Panel2.Controls.Clear();
            splitContainer1.Panel2.Controls.Add(f1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            f2.Show();
            splitContainer1.Panel2.Controls.Clear();
            splitContainer1.Panel2.Controls.Add(f2);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            f3.Show();
            splitContainer1.Panel2.Controls.Clear();
            splitContainer1.Panel2.Controls.Add(f3);
        }

3、实现效果
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大浪淘沙胡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值