.net – host a Windows Form inside a control


I have a customer which as a Visual Basic Project in single instance mode with a wired presentation logic.

The main form contains a TabControl with mutliple TabPages.
If I click on TabPageA another form is shown in front of the Form and resized to have the same size as the TabPage.

If I click on TabPageB the first form is hidden and another form is displayed.
So basically for the user it looks like you have a TabControl with different TabPages which is not the case.

I tried converting the Forms to UserControls and put them inside the TabPage, but, thanks to the SingleInstance app, this would take a whole lot of refactoring. I tried it but eventually gave up because of many many runtime errors and I don't want to put any more effort in this.

My Ideam was that, at runtime, I could add the forms to the TabPages and let them act like UserControls, is this even possible?

Best Solution

You can turn a Form class back to a child control by setting its TopLevel property to False. It becomes essentially a UserControl with some unused overhead. Make it look similar to this:

Public Class Form1
    Public Sub New()
        Dim frm As New Form2
        frm.TopLevel = False
        frm.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        frm.Visible = True
        frm.Dock = DockStyle.Fill
    End Sub
End Class
Related Question