Embed a form onto a tabcontrol in windows forms

后端 未结 5 1592
你的背包
你的背包 2020-12-11 03:36

I have a tab control in a windows form and I want to be able to click on a tab and in the body area of the tab I want it to display another form as an embedded component. I

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-11 04:04

    Set your MainForm (Parent) as IsMDIContainer = true;

    Create an instance of the ChildForm and call this function:

    FormChild frmChild = new FormChild();
    AddNewTab(frmChild);
    

    Copy this Function to your code:

    private void AddNewTab(Form frm)
    {
    
        TabPage tab = new TabPage(frm.Text);
    
        frm.TopLevel = false;
    
        frm.Parent = tab;
    
        frm.Visible = true;
    
        tabControl.TabPages.Add(tab);
    
        frm.Location = new Point((tab.Width - frm.Width) / 2, (tab.Height - frm.Height) / 2);
    
        tabControl.SelectedTab = tab;
    
    }
    

提交回复
热议问题