Home Label text not changing when it is supposed to
Reply: 0

Label text not changing when it is supposed to

user7156
1#
user7156 Published in August 15, 2018, 9:02 am

My full code is below. Basically when a user attempts to login, the screen shakes and a label shows "Connecting to SQL. Please wait..." first and then "Invalid Credentials" next. After typing in incorrect credentials, what's supposed to happen is the label ONLY shows "Connecting to SQL. Please wait..." but what IS happening is that this text is never displayed and just shows my other form. I can't figure out why. Can someone help?

Imports System.ComponentModel
Imports MySql.Data.MySqlClient
Public Class frmSplash
    Dim UsernameFirstLoad As Boolean = True
    Dim PasswordFirstLoad As Boolean = True
    Dim UsernameGotFocus As Boolean = False
    Dim PasswordGotFocus As Boolean = False
    Dim UserValidated As Boolean = False
    Dim FormFirstLoad As Boolean = True
    Dim Username, Password As String

    Dim DS As New DataSet
    WithEvents credentialsBS As New BindingSource


    Private Sub frmSplash_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TableLayoutPanel1.BackColor = ColorTranslator.FromHtml("#2B2B2B")
        lblLoginStatus.BackColor = ColorTranslator.FromHtml("#2B2B2B")
        'lblLoginStatus.ForeColor = Color.LimeGreen
        'lblLoginStatus.Text = "Connecting to SQL. Please wait..."
        Logo.Focus()
    End Sub
    Private Sub frmSplash_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        Logo.Focus()
        Dim x As Integer = CInt((Panel1.Size.Width - txtUsername.Size.Width) / 2)
        Dim x2 As Integer = CInt((Panel3.Size.Width - btnLogin.Size.Width) / 2)
        With DS
            .Tables.Add(New DataTable With {.TableName = "Credentials"})
        End With

        txtUsername.Location = New Point(x, txtUsername.Location.Y)
        txtPassword.Location = New Point(x, txtPassword.Location.Y)
        Label1.Location = New Point(txtUsername.Location.X - 2, txtUsername.Location.Y - 2)
        Label2.Location = New Point(txtPassword.Location.X - 2, txtPassword.Location.Y - 2)
        btnLogin.Location = New Point(x2, btnLogin.Location.Y)
        'lblLoginStatus.Location = New Point(x2, lblLoginStatus.Location.Y)

        txtUsername.Text = "Username"
        txtPassword.Text = "Password"
    End Sub
    Private Sub txtUsername_Click(sender As Object, e As EventArgs) Handles txtUsername.Click
        If UsernameFirstLoad = True Then
            txtUsername.Clear()
            txtUsername.ForeColor = Color.Black
            UsernameFirstLoad = False
        End If
    End Sub
    Private Sub txtPassword_Click(sender As Object, e As EventArgs) Handles txtPassword.Click
        If PasswordFirstLoad = True Then
            txtPassword.Clear()
            txtPassword.PasswordChar = CChar("•")
            txtPassword.ForeColor = Color.Black
            PasswordFirstLoad = False
        End If
    End Sub
    Private Sub txtUsername_GotFocus(sender As Object, e As EventArgs) Handles txtUsername.GotFocus
        If UsernameGotFocus = True Then
            txtUsername.Clear()
            txtUsername.ForeColor = Color.Black
        Else
            UsernameGotFocus = True
        End If
    End Sub
    Private Sub txtUsername_LostFocus(sender As Object, e As EventArgs) Handles txtUsername.LostFocus
        If txtUsername.Text = "" Then
            txtUsername.ForeColor = Color.DimGray
            txtUsername.Text = "Username"
        End If
    End Sub
    Private Sub txtPassword_GotFocus(sender As Object, e As EventArgs) Handles txtPassword.GotFocus
        'If PasswordGotFocus = False Then
        txtPassword.Clear()
        txtPassword.ForeColor = Color.Black
        txtPassword.PasswordChar = CChar("•")
        'PasswordGotFocus = True
        'End If
    End Sub
    Private Sub txtPassword_LostFocus(sender As Object, e As EventArgs) Handles txtPassword.LostFocus
        If txtPassword.Text = "" Then
            txtPassword.ForeColor = Color.DimGray
            txtPassword.Text = "Password"
            txtPassword.PasswordChar = Nothing
        End If
    End Sub
    Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
        lblLoginStatus.ForeColor = Color.LimeGreen
        lblLoginStatus.Text = "Connecting to SQL. Please wait..."
        System.Threading.Thread.Sleep(50)

        Logo.Focus()
        Username = txtUsername.Text.Trim
        Password = txtPassword.Text.Trim
        Cursor.Current = Cursors.WaitCursor
        btnLogin.Enabled = False
        'txtUsername.ReadOnly = True
        'txtPassword.ReadOnly = True
        bwLogin.RunWorkerAsync()
    End Sub
    Private Sub bwLogin_DoWork(sender As Object, e As DoWorkEventArgs) Handles bwLogin.DoWork
        If FormFirstLoad = True Then
            Using MySQLConn As New MySqlConnection("server=1.1.1.1;user id=username;password=12345;persistsecurityinfo=True;database=database")
                Using loginDA As New MySqlDataAdapter("SELECT * FROM credentials", MySQLConn)
                    loginDA.MissingSchemaAction = MissingSchemaAction.AddWithKey
                    loginDA.FillSchema(DS.Tables("Credentials"), SchemaType.Source)
                    loginDA.Fill(DS.Tables("Credentials"))
                End Using
            End Using
            FormFirstLoad = False
        End If
    End Sub
    Private Sub bwLogin_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles bwLogin.RunWorkerCompleted
        credentialsBS.DataSource = DS.Tables("Credentials")
        lblLoginStatus.ForeColor = Color.LimeGreen
        lblLoginStatus.Text = "Connecting to SQL. Please wait..."
        lblLoginStatus.BringToFront()
        lblLoginStatus.Visible = True
        Dim idx As Integer
        Dim idy As Integer
        With credentialsBS
            idx = .Find("username", Username)
            If idx >= 0 Then
                idy = .Find("password", Password)
                If idy >= 0 Then
                    'lblLoginStatus.ForeColor = Color.LimeGreen
                    'lblLoginStatus.Text = "Connecting to SQL. Please wait..."
                    LoginSuccess()
                Else
                    LoginFailed()
                End If
            Else
                LoginFailed()
            End If
        End With
    End Sub
    Private Sub LoginSuccess()
        lblLoginStatus.ForeColor = Color.LimeGreen
        lblLoginStatus.Text = "Connecting to SQL. Please wait..."
        lblLoginStatus.Visible = True

        System.Threading.Thread.Sleep(1500)
        frmMain.Show()
    End Sub
    Private Sub LoginFailed()
        'lblLoginStatus.Text = ""
        'lblLoginStatus.Visible = False
        Me.Left += 10
        System.Threading.Thread.Sleep(50)
        Me.Left -= 10
        System.Threading.Thread.Sleep(50)
        Me.Left += 10
        System.Threading.Thread.Sleep(50)
        Me.Left -= 10
        System.Threading.Thread.Sleep(50)

        lblLoginStatus.ForeColor = Color.Red
        lblLoginStatus.Text = "Invalid Credentials!"
        Cursor.Current = Cursors.Default
        btnLogin.Enabled = True
        txtUsername.ReadOnly = False
        txtPassword.ReadOnly = False
    End Sub
End Class
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.298762 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO