11
votes

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?

3
You could try grabbing a copy of the form's control array and pushing it into a new panel on the other control maybe.asawyer
Have you looked at MDI? Maybe you could replace the tab control with menu items so that the main window could have and open child windows from the Menu bar.Jon Raynor

3 Answers

38
votes

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()
        InitializeComponent()
        Dim frm As New Form2
        frm.TopLevel = False
        frm.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        frm.Visible = True
        frm.Dock = DockStyle.Fill
        TabPage1.Controls.Add(frm)
    End Sub
End Class
6
votes

Any window can be hosted in any other window (a Control is a window, technically) using SetParent.

<System.Runtime.InteropServices.DllImport("user32.dll")>
Public Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr

End Function

to declare and

SetParent(FormToHost.Handle, ControlToHostForm.Handle)

to use. It may not be ideal but it's okay if you don't want to put any more effort into this, like you say. The forms will maximize and minimize properly, and will not show up in the taskbar, and will close with their containers.

-1
votes

you can use panels. in each tab different panel or panels must be show an the other or others must be hide.