标签文本在应该更改时不会更改

  • 本文关键字:文本 标签 .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

不确定为什么需要运行此异步,因为在批准之前不会发生任何其他事情。不要为了检查一个用户而提取表中的所有数据(可能是数千条记录)。数据库服务器非常擅长搜索索引和所有索引。让服务器完成工作。请务必使用参数以避免 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

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

最新更新