Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed a form onto a tabcontrol in windows forms

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. Is this possible? If so, can someone please provide an example or a link to an example of how to accomplish this?

like image 688
MBU Avatar asked Apr 26 '11 20:04

MBU


People also ask

How do I add a form in Winforms?

Add a new formRight-click on the project and choose Add > Form (Windows Forms). In the Name box, type a name for your form, such as MyNewForm. Visual Studio will provide a default and unique name that you may use.

How do you access a form control from another form?

A Form is a Control so we can cast any form in your project as a simple Control object. Example; Control c = TheForm("Form1"); Once we have this, we can gain access to ALL the child controls including the children in other container controls on the form.


1 Answers

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;

}
like image 189
Leonardo Cunha Avatar answered Oct 06 '22 00:10

Leonardo Cunha