Solved

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

Posted on 2004-10-19
6
485 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

709 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now