简体   繁体   中英

VB.NET - Form in TabControl not opened after closing It

I have ToolStripMenu from where I open forms. Forms are opened in TabControl, which is placed in one of the Split Container's panel. I have placed a button in Split container too, It closes any selected TabPages(where forms open). Problem is that when I open form in new TabPage and close It by this button, form doesn't open anymore. WHY ? ....Here is my code:

Private Sub SearchItemsAPOToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SearchItemsToolStripMenuItem.Click

       'Define new page in Tab control and form to open in It
        Dim PageNew As New TabPage()
        Dim FrmItem As New Search_Items

        'Define where and how form should open
        FrmItem.TopLevel = False
        FrmItem.Dock = DockStyle.Fill
        FrmItem.FormBorderStyle = FormBorderStyle.None

        'If form allready opened in TabPage, only send focus to It
        If Application.OpenForms().OfType(Of Search_Items).Any Then

            For Each page As TabPage In TabControl1.TabPages

                If page.Text = "Search Items" Then

                    TabControl1.SelectedTab = page

                End If

            Next page

            'If form not allready opened, we open It in Tab control and send focus on that TabPage
        Else

            PageNew.Controls.Add(FrmItem)
            PageNew.Text = "Search Items"
            TabControl1.Visible = True
            TabControl1.TabPages.Add(PageNew)
            FrmItem.Show()
            BtnTab.Visible = True
            TabControl1.SelectedTab = PageNew

        End If

    End Sub


 Private Sub BtnTab_Click(sender As Object, e As EventArgs) Handles BtnTab.Click

        'Button is visible when TabPages are opened, and with click It closes selected TabPage
        Me.TabControl1.TabPages.Remove(Me.TabControl1.SelectedTab)

        'IF no TabPages, button hides again
        If TabControl1.TabPages.Count = 0 Then
            TabControl1.Visible = False
            BtnTab.Visible = False
        End If
    End Sub

The object already exists and is only hidden and you are creating it again, dispose of the object before recreating it.

Dim tbp As TabPage = TabControl1.SelectedTab
TabControl1.TabPages.Remove(tbp)
tbp.Dispose()

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