Kashif
Kashif

Reputation: 31

How Can i Close Child Form when Click on Parent Form?

I am having some trouble in my Windows form application. I have 2 forms:

1st Main / Parent Form

2nd Sub / Child Form (Menu)

Problem:

I want to close my 2nd form (which opens on top of 1st form) when I click on the 1st form.

Upvotes: 0

Views: 1757

Answers (3)

Steven Doggart
Steven Doggart

Reputation: 43743

Something like this should work:

Public Class Form1
    Private childForm As Form2

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        childForm = New Form2()
        childForm.Show()
    End Sub

    Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        If childForm IsNot Nothing Then
            childForm.Hide()
        End If
    End Sub
End Class

Upvotes: 1

Tomtom
Tomtom

Reputation: 9384

When u show your child-form u have to save the child-form in a variable in the parent-form. on a click on the parent-form you can use the variable to close or dispose your child-form

Upvotes: 0

eyossi
eyossi

Reputation: 4330

You can register to the main form "on focus" event (this link is a basic tutorial that shows how to register for events in VB: http://www.homeandlearn.co.uk/net/nets10p1.html)

when that event occurs you will have to check if the second form is open (save a reference to the opened form so you can do that) and then close it.

If you will add some code i will be able to be more specific...

Upvotes: 1

Related Questions