主窗体: 

子窗体:

把子窗体嵌入到主窗体的panel 右侧中:

代码:

{
        public MainForm()
        {
            InitializeComponent();
        }
        private void ClosePreForm()  
        {
            foreach (Control item in this.panelform.Controls)
            {
                if (item is Form)
                {
                    Form objControl = (Form)item;
                    objControl.Close();
                }

            }
        }
        private void OpenForm(Form objFrm)
        {
            //嵌入子窗体到父窗体中,把添加学员信息嵌入到主窗体右侧
            objFrm.TopLevel = false; //将子窗体设置成非最高层,非顶级控件
            objFrm.WindowState = FormWindowState.Maximized;//将当前窗口设置成最大化
            objFrm.FormBorderStyle = FormBorderStyle.None;//去掉窗体边框
            objFrm.Parent = this.panelform;//指定子窗体显示的容器
            objFrm.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //嵌入窗体前判断当前容器中是否有窗口没关掉
            ClosePreForm();
            OpenForm(new ChirdForm());
        }
 
    }

运行效果:

转载于:https://www.cnblogs.com/zuochanzi/p/6102036.html

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐