C# how to embed the subform into the main form to display

C# how to embed the subform into the main form to display

Today I will summarize the method of embedding the subform into the main form. When you click a Button to display a subform, the subform is often popped up separately, and if you want the subform to be displayed directly on the main form, you need to embed the subform into the main form. as follows:

        //在主窗体中实例化要显示的子窗体
        private ChildForm cForm;
        public MainForm()
        {
            InitializeComponent();
            cForm = new ChildForm (this);
        }

        //打开子窗体的方法
        private void OpenChildForm(Form chidForm)
        {
            if (currentChildForm != null)
            {
                currentChildForm.Close();
            }

            currentChildForm = chidForm;
            chidForm.TopLevel = false;
            chidForm.FormBorderStyle = FormBorderStyle.None;//让窗体无边界
            chidForm.Dock = DockStyle.Fill;
            //在主窗体中添加一个Panel控件用来放置子窗体
            panelDesktop.Controls.Add(chidForm);//将子窗体加入到Panel控件中
            panelDesktop.Tag = chidForm;
            chidForm.BringToFront();
            chidForm.Show();//显示子窗体
        }

        //在Button事件中执行方法
        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenChildForm(cForm);
        }

 

Guess you like

Origin blog.csdn.net/Kevin_Sun777/article/details/110230042