'How to automatically login with VB Login form code?

Say username is ADMIN and password is ADMIN. It should be auto LOGIN and it's not showing FORM 3. What would the code be for Visual Basic. Is mine correct?

My form output:

Login form

Here's my example code:

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If TextBox1.Text <> "" And TextBox2.Text <> "" Then
        MsgBox("Invalid username and password, Please try again!", +vbExclamation, +vbOK)


    ElseIf TextBox1.Text = My.Settings.Username And
    TextBox2.Text = My.Settings.Password Then
        MsgBox("Login Successfuly! Good day.", +vbInformation, +vbOK)
        Form3.Show()
        Me.Hide()

    Else
        MessageBox.Show("Please complete the required fields.", "Authentication Error" + TextBox1.Text + TextBox2.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)

    End If
End Sub


Solution 1:[1]

Below code is simple login form button click event code.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       If TextBox1.Text = "rfb" And TextBox2.Text = "reflection" Then
           Form2.Show()
       Else
           MsgBox("Sorry, username or password not found", MsgBoxStyle.OkOnly, "Invalid")
       End If
   End Sub

change code as your needs.

You can change the username and the password in the code where it says "rfb" for the username and "reflection" for the password. - See more at: http://www.visual-basic-tutorials.com/form/LoginT.htm#sthash.yb1ihiFB.dpuf

hope this will help

Solution 2:[2]

I use next code for login:

    Dim user As String = UsernameTextBox.Text
    Dim pass As String = PasswordTextBox.Text
    Dim sts As Integer = 0
    If user = My.Settings.UeserName Then
        sts = sts + 1
    Else
        MsgBox("User name not found , Please,Try again.Or Contect Adminstrtor for help", vbOK, "login error")
    End If
    If pass = My.Settings.Password Then
        sts = sts + 10
    Else
        MsgBox("User name not found , Please,Try again.Or Contect Adminstrtor for help", vbOK, "login error")
    End If

    If sts = 11 Then
        hello.Enabled = Enabled
        Me.Close()
    End If

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mohammed Shiyas
Solution 2 Robot79