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、实现效果