Solved

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

Posted on 2004-10-19
6
498 Views
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()
                form2.show
           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.
0
Comment
Question by:veayou
[X]
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
6 Comments
 
LVL 10

Expert Comment

by:123654789987
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
acc_ptr1.Hide()

// Show parent Form
acc_ptr1.Show()

// Hide child Form
acc_ptr2.Hide()

// Show child Form
acc_ptr2.Show()

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.
0
 
LVL 4

Expert Comment

by:eozz_2000
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()
    Me.AddOwnedForm(frm)
    frm.Show()
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
        Me.Hide()
        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
        Me.Owner.Show()
    End If
End Sub
0
 

Author Comment

by:veayou
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.
0
Industry Leaders: 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!

 
LVL 2

Accepted Solution

by:
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
            frm.Show()
        Else
            frm.Activate()

        End If
0
 
LVL 4

Assisted Solution

by:eozz_2000
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
Next
If Not bolAlready Then
    dim frm2 as new Form2()
    me.AddOwnedForm(frm2)
    frm2.Show()
End If
0
 
LVL 4

Expert Comment

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

Else
    frm.Show()
End if
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

707 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