Exit Button... Need Help!!!

  • Thread starter Thread starter User_of_VB
  • Start date Start date
U

User_of_VB

Guest
Hi All, can you please see this code and tell me what is wrong with it?
I've got two forms (frmMain and frmHelp) i need 2 buttons on frmMain (btnExit, btnHelp) and one button on frmHelp.
Here is my code:

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Dim X As New frmMain()
Me.Close()
Dim Y As New frmHelp()
Y.Close()
End Sub

Private Sub btnHelp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHelp.Click
Dim X As New frmHelp()
X.Show()
Dim A As New frmMain()
A.Visible = False

End Sub
End Class


It runs properly, but the times i press Help button from main form - that many times i need to press button exit on the main form to exit the program... :(

And Here is the code for frmHelp

Private Sub btnMainMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMainMenu.Click
Dim X As New frmMain()
X.Show()
Dim Y As New frmHelp()
Me.Dispose()
End Sub

Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

End Sub
End Class

Fnyone can help ???
Any suggestions would be appreciated!
 
Since you are creating new instances of the form each time you
press the button, you aren't accessing the same instance of the
object to show or hide it.

At the top of your form code of frmMain, declare X and Y as new
instances of frmHelp and frmMain. Then, in the code for your buttons,
you can X.Show(), X.Close(), X.Hide(), or whatever needs to be
done to show or hide the forms.

Also, instead of Me.Dispose(), use Me.Close()

HTH
 
Bucky
I've done the things that you've told me, but now it doesn't run at all :(
What could be wrong??

Public Class frmMain
Inherits System.Windows.Forms.Form
Dim X As New frmMain()
Dim Y As New frmHelp()

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
X.Close()
Y.Close()
End Sub

Private Sub btnHelp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHelp.Click
Y.Show()
X.Hide()
End Sub
End Class

???? I don't know, can you please help me??
Thank you very much!
 
Back
Top