Solved

Cant use a VERY easy and basic form feature in VB.Net, coming from VB6

Posted on 2004-10-16
2
199 Views
Last Modified: 2010-04-23
Well, this is an easy question I suppose, but I couldn't figure out how to use this in VB.NET using MSDN.
Suppose I have 2 forms, each has a button.
I want it so that if I press the button on form1 it will disappear and form2 appear, the same with form2.
When I press the button on form2 it should disappear and form1 appear.

Now, declaring a form2 type variable in form1 and assigning it to in the initialization in the designer code region:
Frm2 = New Form2(Me)

Then a function in form2 would:
Public Sub New(ByVal host As System.Windows.Forms.Form)
        Frm1 = host
End Sub

When Frm1 is type Form1.

Then I used the following fuctions to make the forms appear and disappear, in form1:
Frm2.Show()
Me.Hide()

In form2:
Frm1.Show()
Me.Hide()

Well It opened Form2  as needed and form1 disappeared, but when i pressed the button on Form2 it would just show a blank form, a real NEW form.

How do I make it right?
Thanks
0
Comment
Question by:NightBlader
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 50 total points
ID: 12328043
There are several ways to go about it.  If you only have two forms then you can use the approach below.  If you push the button on either form then the forms will swap.  If you push the close button [X] on either form the application will end.

~IM

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private WithEvents f2 As Form2 = New Form2

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Hide()
        f2.Show()
    End Sub

    Private Sub f2_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles f2.VisibleChanged
        If Not f2.Visible Then
            Me.Show()
        End If
    End Sub

    Private Sub f2_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles f2.Closed
        Me.Close()
    End Sub
End Class

Public Class Form2
    Inherits System.Windows.Forms.Form

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Hide()
    End Sub
End Class
0
 

Author Comment

by:NightBlader
ID: 12328178
Yep,
Thanks, it works.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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
vb.net help 4 34
VB.net Move a class from Solution Items to a project 2 33
Open a link in vb.net 2 16
Code enhancement 4 22
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
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 …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

837 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