Close main form and show another

N

Nathan

I read an earlier post from Gary and answered by Peter Huang concerning
closing one form and showing another. I need to do the same thing in my
application, but the code Peter gave didn't work for me. Maybe Peter or
someone else can help me out.

I changed the startup object to Sub Main, created a new module and inserted
this code:

Imports System.Windows.Forms

Public Module MyApplication
Public formCount As Integer
Public fm1 As Form1
Public fm2 As Form2
Public Sub OnFormClosed(ByVal sender As Object, ByVal e As EventArgs)
formCount = formCount - 1
If (formCount = 0) Then
Application.ExitThread()
End If
End Sub

Public Sub Main()
formCount = 0
fm1 = New Form1
formCount = formCount + 1
fm2 = New Form2
formCount = formCount + 1
AddHandler fm1.Closed, AddressOf OnFormClosed
AddHandler fm2.Closed, AddressOf OnFormClosed
fm1.Show()
Application.Run()
End Sub

End Module

I just changed <Form1> and <Form2> to the names of the two forms with which
I'm working. On each form there is a menu item with a click event that
says:

Me.close
fm2.show
/// or
fm1.show

When I run the application, fm1 appears. I click the menu item, and the fm1
disappears and fm2 appears. I click fm2's menu item, and fm2 disappears and
fm1 appears. However, when I close out of either of the forms, the debugger
continues running, and I have to hit the Stop button to get it to stop.

I have tried replicating this in a brand new application with two forms
named
Form 1 and Form 2, with only a button on each form, and the appropriate
lines in the buttons' click events. I get the same problem.

What's going wrong?
 
A

Armin Zingler

Nathan said:
When I run the application, fm1 appears. I click the menu item, and
the fm1 disappears and fm2 appears. I click fm2's menu item, and fm2
disappears and fm1 appears. However, when I close out of either of
the forms, the debugger continues running, and I have to hit the Stop
button to get it to stop.

I don't know how this can work at all because you can not show a disposed
(means also closed) form again. Maybe you create a new instance of the other
form each time the form is closed?
 
C

Cor

Hi Nathan,

I see always a lot of solution for this, I asume you are not using a MDI.

I don't like modules and also not with sub Main, I cannot tell why but it
is.

I never did something in VB.net with 2 forms in one application other than
dialogforms and webforms. That is because I don't like the work I have to do
to keep them nice aligned.

I did test tomorrow this solution, can you try it, in my test situation it
works very nice and simple. It are 3 forms, on form 1 are two buttons named
button2 and button3 and a label1. And on the other 2 forms both a button1.

I am currious if this works for you also?

Cor

\\\
Option Strict On
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
#End Region
Private WithEvents frm2 As New Form2
Private WithEvents frm3 As New Form3
Private myActiveForm As Integer
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
myActiveForm = 2
frm2.Show()
me.Hide()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button3.Click
myActiveForm = 3
frm3.Show()
me.Hide()
End Sub
Private Sub frm2_VisibleChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles frm2.VisibleChanged,
frm3.VisibleChanged
Me.Show()
Me.Label1.Text = "Return form = " & myActiveForm.ToString
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
///
\\\
Public Class Form3
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
///
 
N

Nathan

Cor,

Thanks, but I got the answer I needed this morning from Herfried's answer to
Gary's question.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top