-1

I'm new to visual basic. I would like to open a new window in VB.NET when I click the log in button. I also wanted to close first the current form so that it will open a new form.

Private Sub btnLogIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogIn.Click
    Dim username As String = tbUsername.Text
    Dim password As String = tbPassword.Text

    If username = "admin" And password = "admin" Then
        MsgBox("Log In Successful!", MsgBoxStyle.Information, "Success")
        Close()
        Dim mainMenu As New MainMenu()
        mainMenu.Show()
    Else
        MsgBox("Log In Failed!" + vbCr + "Wrong credentials!", MsgBoxStyle.Exclamation, "Failed")
    End If

End Sub

When I click the log in button, it will display the next form for about 0.10 seconds then close. I also tried mainMenu.ShowDialog() but it yields the same result. What should I do?

Jande
  • 1,695
  • 2
  • 20
  • 32
Bryan James
  • 65
  • 1
  • 11
  • 2
    Change Project > Properties > Application tab > Shutdown mode. Look around some more, this is supposed to be discoverable. When you see something you don't understand then press F1. – Hans Passant Mar 22 '16 at 07:38

2 Answers2

0

Your problem is you are defining your window form variable inside a method. The garbage collector is deleting your window after short time and thus it's closed. Try to define it outside of your method as private or public variable and initialize it inside.

0

Show the form before close.

Try this:

   Dim Login As New Form
   Login.Show()
   Me.Close()

This question already has an answer here

Community
  • 1
  • 1
Jande
  • 1,695
  • 2
  • 20
  • 32