tags:

views:

2071

answers:

5

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?

+6  A: 

Not directly. You can create a usercontrol, move all of the code from your form to the usercontrol and use this in both forms. You might need to change some of the code from your form but probably not much.

Mendelt
+1 Refactor all commonality into a user control that you can share.
Gishu
I would **-1** for missleading info as it is most definitely POSSIBLE to directly embed a form in another form and he ASKS "Is it possible..." but I have learned, through experience, that it is such a bad idea/nightmare to work with that it is best if people think it isn't possible... ;-)
Refracted Paladin
A: 

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.

jkchong
+2  A: 

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.

ConcernedOfTunbridgeWells
A: 

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.

Refracted Paladin
A: 

Hi here is a link to how to create user controls with code sample. Good link. http://www.dountsis.com/tutorials/dotnet/how-to-use-user-controls-to-create-mdi-winform-applications.php

If you download the code and run it you are able to drag the controls from the toolbox into your winform window.

I have a question though. How can I make these controls act similar to the Media Player control. That is how can I launch a process and direct the output to the user control. If i make a user control start a process, say outlook, it opens a new window and that is not what I want. I want the output of outlook say to be confined to the user control embedded form. I hope this is not a long way to do somthing simple I am not aware of. cheers

Bill E