Closing Forms...

Dreakon

New member
Joined
Mar 2, 2006
Messages
2
Programming Experience
Beginner
I am having a little trouble with my program. What it does is when I click a button on the main form, it opens another form with this code:

VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] btnLaunch_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] btnLaunch.Click
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] frmLaunch [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] frmLaunch
frmLaunch.Show()
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Hide()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

Then, on the next form there is a button to go back to the main form. It uses this code:

VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] btnBack_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] btnBack.Click
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] frmMain [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] frmMain
frmMain.Show()
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Hide()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]


Now, when I try and close the program for good (Me.Close() on the Main form) it still hangs around in memory though the windows are all closed. Closing the program works fine as long as I don't open the other form and go back.

I'm not very good at VB.Net, I dunno how stupid this problem may seem but I guess if anyone can figure it out how to fix this code, it's you guys. ;)
 
that's because you're never closing the main form

what you need to do is add a module to the project and put this line in:
VB.NET:
Friend MainForm As frmMain  'Where frmMain is the name of your main form
now in the load event of the Main form put:
VB.NET:
MainForm = Me
now in the 2nd form change:
VB.NET:
PrivateSub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
Dim frmMain AsNew frmMain
frmMain.Show()
Me.Hide()
EndSub
to:
VB.NET:
PrivateSub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
  MainForm.Show()
  Me.Hide()
End Sub
by doing it this way, you're hiding the Main form when opening the 2nd form then when you're done with the 2nd form you're re-showing the original main form and thus when you close the original main form the whole application closes

now there is a flaw with this techique and that flaw is what if the user clicks the X button to close the 2nd form... the main form isnt shown again so there's a problem here's how to address it...
in the 2nd form use the Closing event to re-show the original Main form:
VB.NET:
Private Sub 2ndForm_Closing(...) Handles MyBase.Closing
  MainForm.Show()
End Sub
 
Back
Top