How can I Show or Hide Form in VB.Net?

Hi experts!

I had learned vb6 and just simple.

-My project contain 2 forms (Form1 and Form2)
-Form1 contain a Button named Button1
-I coded for the button when I click it, Form2 will Show
    The code is that:
           sub button1_click()
           end sub
-When I closed form1, the whole project didn't stop until I close form2

But in VB.Net I tried to make like this but there is no show or hide method in the form. If I create instance for form2 then I can show it but every time I click button1 it will show one more form. It's not what I want. Also when I close form1 the whole project stop not like vb6.

So is there any suggestion to make it work like in vb6?

Thank you.
Who is Participating?
koolnurdConnect With a Mentor Commented:
try this

Dim frm As New Form2() ' this should be at the top and not in click itself other wise every time you will click a new form will generate.

' then in the click of btn write this....
If frm.Visible = False Then

        End If
If u want to hide or show forms u can try this out

parentClass acc_ptr1 = new parentClass();
childClass acc_ptr2 = new childClass();

// Hide parent Form

// Show parent Form

// Hide child Form

// Show child Form

If u close the parent form then the child form also has to close since the instance of the child form was created from the parent form. U should not maintain multiple instances of a child form at any time. A good practise is while creating a instance of the child form , check if a instance of child form is already open. If yes then u have to prompt the user to close the previous form instance.
Well, reading your problem I suggest the following:

In the parent form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim frm As New Form2()
End Sub

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    If Me.OwnedForms.Length > 0 Then
        e.Cancel = True
    End If
End Sub

In the child form:
Private Sub Form2_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    If Not Me.Owner.Visible Then
    End If
End Sub
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

veayouAuthor Commented:

The example of 123456789987 it cannot work. The compiler dont understand the word parentClass and childClass.

For eozz_2000 is nearly what I want but every time I press button1, a new will form will pop up. I just want the same form to show and if it is already show and it is in the back it will go to front.

Thank you.
eozz_2000Connect With a Mentor Commented:
No, I suggest:

-When you create your Form2, give a name that you can remember like "Hello"
-In the event click of the button you can put this:

Dim bolAlready As Boolean
Dim forms() As Form = Me.OwnedForms
Dim frm As New Form()
For Each frm In forms
    If frm.Name = "Hello" Then
        bolAlready = True
    End If
If Not bolAlready Then
    dim frm2 as new Form2()
End If
sorry, in the if Not bolAlready you must put an else statement:

End if
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.