Solved

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

Posted on 2004-10-19
6
489 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
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Single Form 8 42
Get the selected ValueMember of Combobox 5 30
SQL LINE CONTINUATION ISSUE 12 33
export data table to excel in vb.net 10 26
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 …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

772 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