标签文本在应该的时候不会改变

时间:2018-05-16 15:59:53

标签: .net vb.net

我的完整代码如下。基本上,当用户尝试登录时,屏幕会抖动,标签会显示"正在连接到SQL。请等一下......"首先,然后"无效的凭证"下一个。 输入错误的凭据后,应该发生的是标签仅显示"连接到SQL。请等一下......"但是发生的事情是这个文本永远不会显示,只是显示我的另一种形式。我无法弄清楚原因。有人可以帮忙吗?

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

2 个答案:

答案 0 :(得分:0)

知道了!看起来只是添加Me.Refresh解决了这个问题。

答案 1 :(得分:0)

不确定为什么需要运行此Async,因为在批准之前不会发生任何其他事情。不要仅仅为了检查一个用户而下拉表中的所有数据(可能是数千条记录)。数据库服务器非常擅长使用索引进行搜索。让服务器完成工作。请务必使用参数来避免SQL注入。我希望这可以为你简化一些事情。

Private Sub bwLogin_DoWork(sender As Object, e As DoWorkEventArgs) Handles bwLogin.DoWork
        Using MySQLConn As New MySqlConnection("server=1.1.1.1;user id=username;password=12345;persistsecurityinfo=True;database=database")
            'Check for the correct names of your columns in the database. Password would probably need
            'back ticks around it because it might be a reserved word.
            Using cmd As New MySqlCommand("SELECT Count(*) FROM credentials Where `UserName` = @User AND `Password` = @Password;", MySQLConn)
                'check the actual datatypes in your database
                cmd.Parameters.Add("@User", MySqlDbType.VarChar).Value = txtUsername.Text.Trim
                cmd.Parameters.Add("@Password", MySqlDbType.VarChar).Value = txtPassword.Text.Trim
                MySQLConn.Open()
                Dim count As Integer = CInt(cmd.ExecuteScalar)
                MySQLConn.Close()
                If count = 1 Then
                    MessageBox.Show("Success")
                    'Your success code
                Else
                    MessageBox.Show("Failure")
                    'Your failure code
                End If
            End Using
        End Using
End Sub