10

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?

stop-cran
  • 4,229
  • 2
  • 30
  • 47
MBU
  • 4,998
  • 12
  • 59
  • 98
  • 1
    Can you move *other* form contents to user control and embed user control instead? – k.m Apr 26 '11 at 20:59
  • How hard do you think it would be to move 43 forms to user controls? or rather how easy would it be to move forms to user controls? – MBU Apr 26 '11 at 23:58
  • 1
    Anything ranging from *trivial* to *impossible* depending on how much your forms are only forms, or forms with domain logic mixed here and there :) – k.m Apr 27 '11 at 07:22

5 Answers5

10

You are probably looking for Tabbed MDI Child Forms

SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
  • Do you know of any other examples? That one doesn't explain how everything works very well. – MBU Apr 26 '11 at 22:12
  • 1
    This is the actual correct answer, that link shows everything how to make it work. I tested it and it works like a charm. – S.H. Apr 05 '13 at 17:04
  • Just don't forget to set the **height** of the tab control to 24 px – Sal Mar 20 '19 at 18:49
7

You can embed a Form but it's not the best choice.

Better place the contents on UserControls and add that to the TabPage.

H H
  • 263,252
  • 30
  • 330
  • 514
6

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;

}
Leonardo Cunha
  • 211
  • 7
  • 6
  • 1
    I removed frm.Location and added WindowState = Maximized and FormBorderStyle = None. Worked perfectly! – NTDLS Sep 08 '16 at 16:01
4

I think the other answer has the right idea; Tabbed MDI is probably what you want.

There is an approach where you create a UserControl that has the same content as the form and use that on the TabPage.

TabPage myTabPage = new TabPage(sometext);
myUserControl = new myUserControlType();
myUserControl.Dock = DockStyle.Fill;
myTabPage.Controls.Add(myUserControl);
myTabControl.Add(myTabPage);

http://bytes.com/topic/c-sharp/answers/270457-can-i-add-form-tabpage goes into more detail; but I'd look at the MDI stuff first.

LikePod
  • 300
  • 1
  • 4
  • 17
Rob P.
  • 14,921
  • 14
  • 73
  • 109
2

If you do not want to use MDI, you can try to put everything from desired form to user control and add this user control in both form and tab.