Solved

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

Posted on 2004-10-16
2
189 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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 …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

12 Experts available now in Live!

Get 1:1 Help Now