Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have looked in many places, but found no need WHAT
The thing is, I have a TabControl TabControl and within this
obviously have TabPages TabPages and within these forms have, there is
getting my problem (noob), when I open a tab at a time
works fine, but when I leave an open and open the second she opens
form the previous form of tab ...
I am very confused right!
To better understand the project have released ....
for anyone taking the test, the menu button on the tab client opens a query.
products tab button opens a further query.

The code I use to open:

VB
Public Sub opensForm2 ()
        If testa_aberto_frm2 = True Then
            The Dim Form2 f2 = New Form2
            f2.Focus ()
        Else
            The Dim Form2 f2 = New Form2
            f2.TopLevel = False
            Form1.TabControl1.TabPages.Add (f2.Text)
            Form1.TabControl1.SelectedTab.Controls.Add (f2)
            f2.Show ()
            test_open_frm2 = True
        End If
    End Sub

    Public Sub openform3 ()
        If teste_aberto_frm3 = True Then
            Dim f3 as Form3 = New Form3
            f3.Focus ()
        Else
            Dim f3  as Form3 = New Form3 
            f3.TopLevel = False
            Form1.TabControl1.TabPages.Add (f3.Text)
            Form1.TabControl1.SelectedTab.Controls.Add (f3)
            f3.Show ()
            test_open_frm3 = True
        End If
    End Sub

to close:

VB
Private Sub Button1_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim f3 = The New Form3 Form3
        f3.Close ()
        Form1.TabControl1.TabPages.Remove (Form1.TabControl1.SelectedTab)
        test_open_frm3 = False
    End Sub

    Private Sub Button1_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   Dim  f2 as Form2= New Form2
        f2.Close ()
        Form1.TabControl1.TabPages.Remove (Form1.TabControl1.SelectedTab)
        test_open_frm2 = False
    End Sub

I thank

[Edit - added code block]
Posted
Updated 12-Apr-12 4:19am
v2
Comments
Maciej Los 14-Apr-12 12:52pm    
It looks like you want to create mdi apllication. Do you understand it? Have you seen an example?
BillWoodruff 20-Mar-15 4:46am    
I would encourage you to think about NOT putting Forms inside the TabPages of a TabControl.

A Form is a "heavy-weight" object; you can achieve the same functionality using Panels which are "lighter-weight."

I wrote for you a simple class to add and removes tabs (with forms). The solution is not perfect... You need to remember: this is an example!

Steps to achieve a solution:
1) We need an interface[^]
VB
Public Interface IFormsInTabs

    Function AddTab(ByVal frm As Windows.Forms.Form) As Integer 'to add new tab with form inside ;)
    Function RemoveTab() As Integer 'to close current tab

End Interface


2) We need a class[^] which implements methods, functions (and other stuff) from interface.
VB
Public Class TFormsInTabs
    Implements IFormsInTabs

    Private oTc As TabControl = Nothing 'we store a TabControl (from Form) in our class

    Public Sub New(ByVal _tc As TabControl)
        oTc = _tc '
    End Sub

    Public Function AddTab(ByVal frm As System.Windows.Forms.Form) As Integer Implements IFormsInTabs.AddTab
        Dim oTp As TabPage = Nothing 'a tabpage to add into TabControl

        Try
            oTp = New TabPage(frm.Text)
            frm.TopLevel = False
            oTp.Controls.Add(frm)
            frm.Dock = DockStyle.Fill
            frm.Show()
            oTc.TabPages.Add(oTp)
            oTc.SelectedTab = oTp

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error... {AddTab}")

        Finally
            oTp = Nothing
        End Try

    End Function

    Public Function RemoveTab() As Integer Implements IFormsInTabs.RemoveTab
        Try
            'removes current tab
            oTc.TabPages.Remove(oTc.SelectedTab)

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error... {RemoveTab}")
        End Try

    End Function

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub

End Class


3) Implemetation (in the form with TabControl):
VB
Public Class MainFrm

    Dim mTC As IFormsInTabs = Nothing 'declare variable to store our class (using interface)

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        mTC = New TFormsInTabs(Me.TabControl1) 'here we call Public Sub New(Byval _tc As TabControl) in our class

    End Sub

    Private Sub TSMIOpenForm1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMIOpenForm1.Click
        Dim frm As Form1 = New Form1

        mTC.AddTab(frm)
        Me.TabControl1.Refresh()
    End Sub

    Private Sub TSMIOpenForm2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMIOpenForm2.Click
        Dim frm As Form2 = New Form2

        mTC.AddTab(frm)
        Me.TabControl1.Refresh()
    End Sub


    Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
        mTC.RemoveTab()
        Me.TabControl1.Refresh()
    End Sub


    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub

End Class


I hope it will be helpful (if yes, please rate it). If you have any question, please let me know (use "Have a Question ..." widget).
 
Share this answer
 
Comments
BillWoodruff 20-Mar-15 4:42am    
+5 This is obviously first-class work. But, let me ask you to consider that in writing the code "for" the OP you may have missed the opportunity to educate the OP about better practices, and increase their own skills.

I say this with reservations because the fact is I often feel I have learned a great deal studying the working code of other people at a more advanced level.

cheers, Bill
Maciej Los 20-Mar-15 5:07am    
Thank you, Bill. How have you found so old post?
Yes, you're right. Well, giving pepople working code is not good teaching practice, but - sometimes - it's the best what we can do. In this case, OP needs only suggestion, because He have basic knwoledge about programming. As you know, my English is not perfect and i'm afraid i won't be able to write it using words... ;)
Cheers, Maciej
Thanks Sir It Works.
I give 5 star Out Of Five Star.
 
Share this answer
 
Comments
BillWoodruff 20-Mar-15 4:43am    
Please post comments like this as comments on the post you are responding to, not as a "solution."

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900