C#:将子Form加入父Form中

实现的功能:已建立了多个子Form界面,在父Form界面左面,点击不同标题的链接文本,父Form界面右面显示不同的子界面内容。

具体如下:

  1、加入split拆分器控件

  2、在splitControl.panel1中添加不同的链接文本,在splitControl.panel2显示不同界面内容。代码如下:

 public partial class Main : Form
    {
        private Form lastForm = null;
        private frmDepartment department = null;
        private frmRole role = null;
        
        public Main()
        {
            InitializeComponent();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (department == null)
            {
                department = new frmDepartment();
            }
            AddControls(department);
            lastForm = department;
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (role == null)
            {
                role = new frmRole();
            }
            AddControls(role);
            lastForm = role;
        }


        private void AddControls(Form form)
        {
            foreach (Control control in this.splitContainer1.Panel2.Controls)
            {
                lastForm.Controls.Add(control);
            }
            //this.splitContainer1.Panel2.Controls.Clear();
            foreach (Control control in form.Controls)
            {
                this.splitContainer1.Panel2.Controls.Add(control);
            }
        }
View Code

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页