close form from form

hederimiller

Member
Joined
Jul 30, 2005
Messages
6
Programming Experience
1-3
close form from form resolved

i have two forms in my project. The first form is form1, and the Second form is form 2 (username and password log in form). When form1 loads it calls form2. but i want it so that if they close form2, it will also close form1.
 
Last edited:
Most suitable for your needs is probably Application.Exit() that would close entire application i.e.

VB.NET:
[size=2][color=#0000ff]Private [/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] Form2_Closing([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Object[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.ComponentModel.CancelEventArgs) [/size][size=2][color=#0000ff]Handles [/color][/size][size=2][color=#0000ff]MyBase[/color][/size][size=2].Closing
Application.Exit() 
[/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]Sub[/color][/size]
[size=2][color=#0000ff][color=#000000]
[/color][/color][/size]

Cheers ;)
 
It would be preferable to not even create the main form if the login either fails or is cancelled. I would suggest using a Main method as your startup object. In this main method you would display the login form using ShowDialog. If the login fails you would return DialogResult.Abort, if it is cancelled you would return DialogResult.Cancel and if it is succesful you would return DialogResult.OK. If ShowDialog returned OK you would call Application.Run and pass a new instance of the main form class. If it returned Abort or Cancel you would do nothing and the app would exit naturally.
VB.NET:
	Public Sub Main()
		Dim loginForm As New Form2

		If loginForm.ShowDialog() = DialogResult.OK Then
			Application.Run(New Form1)
		End If
	End Sub
You could place this procedure in a module or put it in the main form and make it Shared.
 
Back
Top