Link to home
Start Free TrialLog in
Avatar of apmcph
apmcph

asked on

Add Forms to TabPages dynamically

HI,

Can I Add Windows Forms to TabPages Dynamically? I want my application to show all window forms to TabPages.
My logic is create a Main TabControl and everytime my program open a new form, TabPages will dynamically created in my TabControl and in that i want to place the new form. Is it possible?

Links to tutorials/articles are accepted if it answer my question.

Thanks.

Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

If you have yet created the tab page dinamically, then you just have to dock the form into your tabpage. Just need something like this.
yourTabPage.Controls.Add(yourForm);
yourForm.Dock = DockStyle.Fill;
Avatar of Mike Tomlinson
It won't work unless you set the TopLevel() property of the Form to False:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim frm As New Form1
        frm.TopLevel = False
        frm.Dock = DockStyle.Fill
        frm.FormBorderStyle = Windows.Forms.FormBorderStyle.None

        Dim tp As New TabPage
        tp.Controls.Add(frm)
        frm.Show()

        TabControl1.TabPages.Add(tp)
    End Sub
Avatar of apmcph
apmcph

ASKER

Idle,
 How about if i want to remove tab when i close the form?
Avatar of apmcph

ASKER

I want to have a close button on the top-right side of the Tab.

thanks
Something like...

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim frm As New Form1
        frm.TopLevel = False
        frm.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedToolWindow
        frm.Dock = DockStyle.Fill
        AddHandler frm.FormClosed, AddressOf frm_FormClosed

        Dim tp As New TabPage
        frm.Tag = tp
        tp.Controls.Add(frm)
        frm.Show()

        TabControl1.TabPages.Add(tp)
    End Sub
Avatar of apmcph

ASKER

Idle,
Yes it has close button but only the form were closed, and not remove the TabPage where the form contained.

thanks
That's because I forgot to include some code for ya...       sorry!

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim frm As New Form1
        frm.TopLevel = False
        frm.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedToolWindow
        frm.Dock = DockStyle.Fill
        AddHandler frm.FormClosed, AddressOf frm_FormClosed

        Dim tp As New TabPage
        frm.Tag = tp
        tp.Controls.Add(frm)
        frm.Show()

        TabControl1.TabPages.Add(tp)
    End Sub

    Private Sub frm_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs)
        TabControl1.TabPages.Remove(sender.tag)
    End Sub
Avatar of apmcph

ASKER

Idle,

How do i  do that in c#? I think i need to implement delegate and event for this.
Sorry im new to windows application using .net/c#.

thanks.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial