views:

1020

answers:

3

I have a a .NET 3.5 windows form which I'd like to embed into another form. Is there a quick way to turn that form into a control?

Thanks

+9  A: 

Change the form to inherit from UserControl instead of Form, then fix any compile errors.

Neil Barnwell
Just found this answer while tackling this same issue myself. Awesome how easy that is.
Dan Tao
+3  A: 

There's also a way to embed a form in a control: Here's the code in VB:

Public Shared Sub ShowFormInControl(ByRef ctl As Control, ByRef frm As Form)
    If ctl IsNot Nothing AndAlso frm IsNot Nothing Then
        frm.TopLevel = False
        frm.FormBorderStyle = FormBorderStyle.None
        frm.Dock = DockStyle.Fill
        frm.Visible = True
        ctl.Controls.Add(frm)
    End If
End Sub

I think I acquired this code from another post on SO, but I can't remember where, so sorry if this is your code snippet!

Joey
Same thing works in C#, obviously.
GWLlosa
I think you'd have to change some things for it to work in C#, otherwise the compiler would be angry...
Joey
A: 

Not saying that you should do this now but in the future you can take a look at MEF. Its a framework for (among other things) building composite applications which it sounds like might be what you're trying to achieve.

George Mauer