简体   繁体   中英

.NET: How to convert a windows form to a windows control

I have aa .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

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

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!

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.

I used @Neil Barnwell's solution, with an addition. I manually edited the.vbprog file and changed the form's "SubType" to "UserControl":

<SubType>UserControl</SubType>

This allows the icon in the project explorer to show as a User Control, rather than a form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM