Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8013795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:43:48+00:00 2026-06-04T19:43:48+00:00

I have a question about closing and disposing a hidden child form. Parent form

  • 0

I have a question about closing and disposing a hidden child form.

Parent form with two buttons:

Public Class Form1
    Dim F2 As Form2

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        F2 = New Form2
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        F2.Show()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        F2.Hide()
    End Sub
End Class

Child form:

Public Class Form2
    Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            e.Cancel = True
            Me.Hide()
    End Sub

    Private Sub Form2_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.VisibleChanged
        MsgBox("Form2.Visible = " & Me.Visible.ToString)
    End Sub

    Private Sub Form2_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        MsgBox("Form2 has been disposed.")
    End Sub
End Class

As long as Form1 is open, I don’t want to close Form2. So that part works.
But I do want to close Form2 when Form1 is closed. Do I have to close it explicitly from Form1? And add more logic to Form2_FormClosing()?
As is, Form2_Disposed() never gets called as far as I can tell (I never get the message box). Is this correct?
When Form1 is disposed, variable F2 no longer exists. Will the Form2 be disposed by the garbage collector later on or not?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:43:51+00:00Added an answer on June 4, 2026 at 7:43 pm

    I will try to move the Form2_FormClosing event to the Form1 class.
    In this way you will be able to control the closing of the Form2 instance from the Form1 instance.

    ' global instance flag
    Dim globalCloseFlag As Boolean = False
    
    ...
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load  
        F2 = New Form2  
        AddHandler F2.FormClosing, New FormClosingEventHandler(AddressOf Form2ClosingHandler)
    End Sub  
    
    ' This will get the event inside the form1 instance, you can control the close of F2 from here
    Private Sub Form2ClosingHandler(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) 
        if globalCloseFlag = false then    
            e.Cancel = True    
            F2.Hide()
        end if
    
    End Sub 
    
    ' Form1 closing, call the close of F2
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing      
          globalCloseFlag = True
          F2.Close()
    End Sub      
    

    Please, pay attention that this is an example, you need to handle special cases like windows shutdown using the FormClosingEventArgs.CloseReason property

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about how to set up the relations between two models
I have question about normalization. Suppose I have an applications dealing with songs. First
I have question about interpreting strings as packed binary data in C++. In python,
i have question about YAJLiOS parser... I have next json data : {{ body
I have a question about whether or not a specific way of applying of
I have a question about django content_types In the example of filtering a QuerySet
I have a question about SQL Server indexes. I'm not a DBA and assume
I have a question about the windows invariant culture. Succinctly, my question is: does
I have a question about JSF and GET http request. How can I get
I have a question about wiring web server controls. From many of the examples

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.