'Embedding a winform within a winform (c#)

Is it possible to embed a windows form within another windows form?

I have created a windows form in Visual Studio along with all its associated behaviour.

I now want to create another windows form containing a tab view, and I want to embed the first windows form into the tab view. Is this possible?



Solution 1:[1]

Disclaimer

This will work as I am using it in my application extensively. That being said I would pursue the User Control route as depending on how far you carry the embedding things start to flake out. FYI


Yes this is possible. This is how:

public static void ShowFormInContainerControl(Control ctl, Form frm)
{
    frm.TopLevel = false;
    frm.FormBorderStyle = FormBorderStyle.None;
    frm.Dock = DockStyle.Fill;
    frm.Visible = true;
    ctl.Controls.Add(frm);
}

I have that in a Class Library and then I call it like so from the FORM I want to embed.

public FrmCaseNotes FrmCaseNotes;
FrmCaseNotes = new FrmCaseNotes();
WinFormCustomHandling.ShowFormInContainerControl(tpgCaseNotes, FrmCaseNotes);

Where tpgCaseNotes is the control I want Form FrmCaseNotes embedded in.
In this case a tab page on the Form I am calling from.

Solution 2:[2]

The way to do this is with a user control rather than a form. This is what user controls are for. This technique can be used for quite a lot of user interface tricks such as wizards (the controls can be shared between the wizard and other parts of the application), explorer style browsers with a tree control and controls swapped out based on the selected node.

I have done quite a lot of work with application architectures that use user controls for everything and frameworks for explorers, wizards and other types of forms (even going back to VB6). As an approach, it works very well.

Solution 3:[3]

let's say you have 2 projects win1 and win2. both are winform projects. you look for embeding win2 in win1.

solution:

open the win2 project and change the output type to "Class Library" (in Application tab)

open the project win1, and add the win2 dll project as a ref in win1 project go in the win1 code, and put this :

        win2.Form1 formI = new win2.Form1();
        formI.TopLevel = false;
        formI.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        formI.Size = this.Size;
        formI.BringToFront();
        formI.Visible = true;
        this.Controls.Add(formI);

Solution 4:[4]

You could try the SetParent() API call, although I have not verified that it would work myself. If that does not work, Mendlet's solution above is probably your best option.

Solution 5:[5]

using System.Runtime.InteropServices;
class EmbedForm{
    [DllImport("user32.dll")]
    public extern IntPtr SetParent(IntPtr hWndChild, hWndNewParent);
    //code, code, code...
}

Usage: EmbedForm.SetParent(ChildForm.Handle, ParentForm.Handle)

Solution 6:[6]

Best Way to do it, is to Create a UserControl or the easy way, make a panel, and turn on Autoscroll on the panel's properties and put the elements in your form of choice into the panel, you might need to change some code, but it's the most easy method.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jimi
Solution 2 ConcernedOfTunbridgeWells
Solution 3 GEOCHET
Solution 4 jkchong
Solution 5 Proger
Solution 6 Jam the adventurer Plays