Click here to Skip to main content
15,908,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.
I have a normal form as main form with a TabControl in it. When I opened a child form, I put it into a TabPage and add the TabPage into the TabControl.
I have many child forms containing a close form button. Users may press the button to close the form. But if this form is shown in a TabPage, after the form is being closed, the TabPage will be blank. It may not be good.
How can I remove the TabPage when I close the child form at the same time?
Posted

1 solution

if i understand you correctly

VB
Sub addFormToTab()
'add the form to your tab
        Dim frm As New Form
        frm.TopLevel = False
        frm.Parent = TabPage1
        frm.Size = TabPage1.Size
        frm.StartPosition = FormStartPosition.CenterParent

'add a handler for form closed event, specific to the form you just added
        AddHandler frm.FormClosed, AddressOf CloseTabThatBelongsToSender 

'show the form on your tab
       ''P.S. I just learned how to add forms to tabs. Good concept, never thought of it.
        frm.Show()
    End Sub
  

'this is the sub routine that your event handler refers to
    Sub CloseTabThatBelongsToSender(Sender As Form, e As System.Windows.Forms.FormClosedEventArgs)
 'declare a variable of tab page type and set it equal to 
'the parrent of the for that threw the event
        Dim x As TabPage = Sender.Parent

'you need the parent to be declared as a TabPage(which is what it is) so that you can do this
        TabControl1.TabPages.Remove(x)

    End Sub




I do love these Object Oriented, Event Driven applications.....
 
Share this answer
 
Comments
Momoko Asahina 708H 6-Aug-13 20:58pm    
Thank you for understanding my requests and answering! It works well!
Mr.TMG 8-Aug-13 22:00pm    
Glad to help friend!

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