Solved

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

Posted on 2004-10-16
2
168 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
I need C# converted to VB.net 1 53
Custom auto number 6 51
Crystal Report for VS2013 17 42
Tabcontrol Caption? 1 24
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

18 Experts available now in Live!

Get 1:1 Help Now