📄 login.vb
字号:
Imports System.Data.SqlClient
Imports System.Threading.Thread
Imports System.Threading.Monitor
Public Class login
Inherits System.Windows.Forms.Form
Public cn As New SqlConnection("server=localhost;database=lms;uid=sa;password=;")
Public da As SqlDataAdapter
Public ds As DataSet
Public cmd As New SqlCommand
Public t1 As New Threading.Thread(AddressOf connect)
Public Shared state As Integer = 0
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents txt_username As System.Windows.Forms.TextBox
Friend WithEvents txt_password As System.Windows.Forms.TextBox
Friend WithEvents btn_ok As System.Windows.Forms.Button
Friend WithEvents btn_cancel As System.Windows.Forms.Button
Friend WithEvents lbl_username As System.Windows.Forms.Label
Friend WithEvents lbl_password As System.Windows.Forms.Label
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents ProgressBar1 As System.Windows.Forms.ProgressBar
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.txt_username = New System.Windows.Forms.TextBox
Me.txt_password = New System.Windows.Forms.TextBox
Me.btn_ok = New System.Windows.Forms.Button
Me.btn_cancel = New System.Windows.Forms.Button
Me.lbl_username = New System.Windows.Forms.Label
Me.lbl_password = New System.Windows.Forms.Label
Me.Label1 = New System.Windows.Forms.Label
Me.ProgressBar1 = New System.Windows.Forms.ProgressBar
Me.SuspendLayout()
'
'txt_username
'
Me.txt_username.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.txt_username.Location = New System.Drawing.Point(119, 32)
Me.txt_username.Name = "txt_username"
Me.txt_username.ReadOnly = True
Me.txt_username.Size = New System.Drawing.Size(104, 21)
Me.txt_username.TabIndex = 3
Me.txt_username.Text = "Librarian"
'
'txt_password
'
Me.txt_password.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.txt_password.Location = New System.Drawing.Point(119, 64)
Me.txt_password.MaxLength = 10
Me.txt_password.Name = "txt_password"
Me.txt_password.PasswordChar = Microsoft.VisualBasic.ChrW(42)
Me.txt_password.Size = New System.Drawing.Size(104, 21)
Me.txt_password.TabIndex = 0
Me.txt_password.Text = ""
'
'btn_ok
'
Me.btn_ok.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btn_ok.Location = New System.Drawing.Point(79, 104)
Me.btn_ok.Name = "btn_ok"
Me.btn_ok.Size = New System.Drawing.Size(56, 23)
Me.btn_ok.TabIndex = 1
Me.btn_ok.Text = "OK"
'
'btn_cancel
'
Me.btn_cancel.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btn_cancel.Location = New System.Drawing.Point(151, 104)
Me.btn_cancel.Name = "btn_cancel"
Me.btn_cancel.Size = New System.Drawing.Size(56, 23)
Me.btn_cancel.TabIndex = 2
Me.btn_cancel.Text = "Cancel"
'
'lbl_username
'
Me.lbl_username.AutoSize = True
Me.lbl_username.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lbl_username.ForeColor = System.Drawing.Color.Navy
Me.lbl_username.Location = New System.Drawing.Point(42, 32)
Me.lbl_username.Name = "lbl_username"
Me.lbl_username.Size = New System.Drawing.Size(72, 17)
Me.lbl_username.TabIndex = 4
Me.lbl_username.Text = "Username : "
'
'lbl_password
'
Me.lbl_password.AutoSize = True
Me.lbl_password.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lbl_password.ForeColor = System.Drawing.Color.Navy
Me.lbl_password.Location = New System.Drawing.Point(47, 64)
Me.lbl_password.Name = "lbl_password"
Me.lbl_password.Size = New System.Drawing.Size(70, 17)
Me.lbl_password.TabIndex = 5
Me.lbl_password.Text = "Password : "
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.BackColor = System.Drawing.Color.Silver
Me.Label1.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label1.ForeColor = System.Drawing.Color.DarkRed
Me.Label1.Location = New System.Drawing.Point(16, 152)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(190, 19)
Me.Label1.TabIndex = 5
Me.Label1.Text = "Connecting...please wait"
'
'ProgressBar1
'
Me.ProgressBar1.Location = New System.Drawing.Point(16, 176)
Me.ProgressBar1.Name = "ProgressBar1"
Me.ProgressBar1.Size = New System.Drawing.Size(248, 12)
Me.ProgressBar1.TabIndex = 6
'
'login
'
Me.AcceptButton = Me.btn_ok
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.Color.Silver
Me.ClientSize = New System.Drawing.Size(280, 205)
Me.Controls.Add(Me.ProgressBar1)
Me.Controls.Add(Me.lbl_password)
Me.Controls.Add(Me.lbl_username)
Me.Controls.Add(Me.btn_cancel)
Me.Controls.Add(Me.btn_ok)
Me.Controls.Add(Me.txt_password)
Me.Controls.Add(Me.txt_username)
Me.Controls.Add(Me.Label1)
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "login"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Login"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btn_ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ok.Click
Try
da = New SqlDataAdapter("select Pwd from user_auth", cn)
ds = New DataSet
'fetching user information from databse
da.Fill(ds, "user_auth")
If txt_password.Text <> "" Then 'blank password is not allowed
If ds.Tables(0).Rows.Count <> 0 Then
'checking password
If txt_password.Text = ds.Tables(0).Rows(0)(0) Then
'enabling MDI form menus
Me.MdiParent.Menu.MenuItems(0).Enabled = True
Me.MdiParent.Menu.MenuItems(1).Enabled = True
Me.MdiParent.Menu.MenuItems(2).Enabled = True
Me.MdiParent.Menu.MenuItems(3).Enabled = True
Me.MdiParent.Menu.MenuItems(4).Enabled = True
Me.Dispose()
Else
MessageBox.Show("Wrong password,try again !!", "LMS", MessageBoxButtons.OK, MessageBoxIcon.Error)
txt_password.Clear()
txt_password.Focus()
End If
Else
If txt_password.Text = "password" Then
Me.MdiParent.Menu.MenuItems(0).Enabled = True
Me.MdiParent.Menu.MenuItems(1).Enabled = True
Me.MdiParent.Menu.MenuItems(2).Enabled = True
Me.MdiParent.Menu.MenuItems(3).Enabled = True
Me.MdiParent.Menu.MenuItems(4).Enabled = True
cmd = New SqlCommand
cmd.Connection = cn
cmd.CommandText = "insert into user_auth values('Librarian','password');insert into options values(1,1)"
cmd.ExecuteNonQuery()
Me.Dispose()
Else
MessageBox.Show("Wrong password,try again !!", "LMS", MessageBoxButtons.OK, MessageBoxIcon.Error)
txt_password.Clear()
txt_password.Focus()
End If
End If
Else 'if password is blank set focus back to the control
txt_password.Focus()
End If
Catch err As SqlException
MessageBox.Show(err.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
Catch ers As System.Exception
MessageBox.Show(ers.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
'dereferencing objects
da = Nothing
ds = Nothing
cmd = Nothing
End Try
End Sub
Private Sub btn_cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_cancel.Click
End
End Sub
Private Sub login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Show()
Application.DoEvents()
btn_ok.Visible = False
btn_cancel.Visible = False
txt_password.Visible = False
txt_username.Visible = False
lbl_password.Visible = False
lbl_username.Visible = False
t1.Start()
While (True)
If ProgressBar1.Value <> 100 Then
ProgressBar1.Value = ProgressBar1.Value + 10
Threading.Thread.CurrentThread.Sleep(200)
End If
If state = 1 Then
ProgressBar1.Value = 100
Label1.Text = "Connected!"
btn_ok.Visible = True
btn_cancel.Visible = True
txt_password.Visible = True
txt_username.Visible = True
lbl_password.Visible = True
lbl_username.Visible = True
Me.Size = New System.Drawing.Size(288, 180)
Exit While
ElseIf state = 2 Then
Exit While
End If
End While
End Sub
Sub connect()
Try
cn.Open()
state = 1
Catch ex As System.Exception
state = 2
Label1.Text = "Not Connected!"
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End
End Try
End Sub
Private Sub login_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
End
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -