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

Posted on 2004-10-19
Last Modified: 2008-02-26

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.
Question by:veayou
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 10

Expert Comment

ID: 12354990
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.

Expert Comment

ID: 12355127
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

Author Comment

ID: 12355527

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.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Accepted Solution

koolnurd earned 400 total points
ID: 12355639
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

Assisted Solution

eozz_2000 earned 100 total points
ID: 12361926
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

Expert Comment

ID: 12361940
sorry, in the if Not bolAlready you must put an else statement:

End if

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
FInd Image Control Gridview 3 35
Import MySQL data into MS Access using VB.Net interface 5 51 Open video relating to control 2 30 convert long time to mm:ss 23 21
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question