473,466 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Close main form and show another

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?

Nov 20 '05 #1
3 2235
"Nathan" <nk*********************@softhome.net> schrieb
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?
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2
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
///


Nov 20 '05 #3
Cor,

Thanks, but I got the answer I needed this morning from Herfried's answer to
Gary's question.
"Cor" <no*@non.com> wrote in message
news:OE**************@TK2MSFTNGP10.phx.gbl...
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
///

Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: chanchito_cojones | last post by:
hi there, I am needing some help with a database I am putting together. The database works off of a main Form, which then has buttons on it that will open up other forms. The problem I am having...
5
by: Viper | last post by:
.... say, like MS Word opens another instance of himself when you open another document. If you close the first 'Word', the second document remains alive. I can't figure out how to do the same...
3
by: Gary | last post by:
Hi, I have my main form1 and I want to close it and then show form2, in VB6 I used the following code... unload me form2.show in VB.net I got this far
7
by: Alice | last post by:
Hi, In my program, the user can navigate to many different forms. When they go to the next form, I want the form they have left to close. However, the forms aren't closing. Can anyone tell me...
10
by: Alex | last post by:
For example, I create Login form first. When user login, it open the main form and close the login form itself.
3
by: Karan | last post by:
I am calling finalize when form2 loads and deactivates form1 which closes form1. However, same thing is not happening in form2 because finalize is already called. Does anybody has solution to it....
2
by: Claudia Fong | last post by:
Hello everybody, I have a Menu form where I have a button. The user should click the button and the program should open another form call register form. I want that when the program show the...
4
by: Jason Huang | last post by:
Hi, In my C# windows form project, how do I in a formA open another formB, and close formA when formB is opened? Thanks for help. Jason
3
by: godhulirbalaka | last post by:
Dear Sir/Madam, I am new vb 6.0 user. I am developing Shop Management Program. I have a main form with buttons and menus. when i click any button then respective form is open. I want to set a...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.