📄 frmlogin.vb
字号:
Imports System.Data.SqlClient
Public Class frmLogin
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
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
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents btnOK As System.Windows.Forms.Button
Friend WithEvents btnCancel As System.Windows.Forms.Button
Friend WithEvents txtPwd As System.Windows.Forms.TextBox
Friend WithEvents cmbID As System.Windows.Forms.ComboBox
Friend WithEvents Label4 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmLogin))
Me.btnOK = New System.Windows.Forms.Button
Me.btnCancel = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.txtPwd = New System.Windows.Forms.TextBox
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.cmbID = New System.Windows.Forms.ComboBox
Me.Label4 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'btnOK
'
Me.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK
Me.btnOK.Font = New System.Drawing.Font("新宋体", 11.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
Me.btnOK.ForeColor = System.Drawing.Color.FromArgb(CType(0, Byte), CType(0, Byte), CType(192, Byte))
Me.btnOK.Location = New System.Drawing.Point(125, 222)
Me.btnOK.Name = "btnOK"
Me.btnOK.Size = New System.Drawing.Size(72, 32)
Me.btnOK.TabIndex = 2
Me.btnOK.Text = "确 定"
'
'btnCancel
'
Me.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.btnCancel.Font = New System.Drawing.Font("新宋体", 11.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
Me.btnCancel.ForeColor = System.Drawing.Color.FromArgb(CType(0, Byte), CType(0, Byte), CType(192, Byte))
Me.btnCancel.Location = New System.Drawing.Point(229, 222)
Me.btnCancel.Name = "btnCancel"
Me.btnCancel.Size = New System.Drawing.Size(72, 32)
Me.btnCancel.TabIndex = 3
Me.btnCancel.Text = "取 消"
'
'Label1
'
Me.Label1.Font = New System.Drawing.Font("楷体_GB2312", 22.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
Me.Label1.ForeColor = System.Drawing.Color.Red
Me.Label1.Location = New System.Drawing.Point(77, 24)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(288, 40)
Me.Label1.TabIndex = 0
Me.Label1.Text = "图形化订餐管理系统"
'
'Label2
'
Me.Label2.Font = New System.Drawing.Font("宋体", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
Me.Label2.ForeColor = System.Drawing.Color.Navy
Me.Label2.Location = New System.Drawing.Point(126, 97)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(89, 16)
Me.Label2.TabIndex = 1
Me.Label2.Text = "用 户 名:"
'
'Label3
'
Me.Label3.Font = New System.Drawing.Font("宋体", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
Me.Label3.ForeColor = System.Drawing.Color.Navy
Me.Label3.Location = New System.Drawing.Point(126, 152)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(89, 16)
Me.Label3.TabIndex = 2
Me.Label3.Text = "密 码:"
'
'txtPwd
'
Me.txtPwd.Location = New System.Drawing.Point(212, 149)
Me.txtPwd.Name = "txtPwd"
Me.txtPwd.PasswordChar = Microsoft.VisualBasic.ChrW(42)
Me.txtPwd.Size = New System.Drawing.Size(152, 21)
Me.txtPwd.TabIndex = 0
Me.txtPwd.Text = ""
'
'PictureBox1
'
Me.PictureBox1.BackColor = System.Drawing.Color.White
Me.PictureBox1.Image = CType(resources.GetObject("PictureBox1.Image"), System.Drawing.Image)
Me.PictureBox1.Location = New System.Drawing.Point(2, 73)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(114, 142)
Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
Me.PictureBox1.TabIndex = 7
Me.PictureBox1.TabStop = False
'
'cmbID
'
Me.cmbID.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
Me.cmbID.Location = New System.Drawing.Point(212, 91)
Me.cmbID.Name = "cmbID"
Me.cmbID.Size = New System.Drawing.Size(152, 20)
Me.cmbID.Sorted = True
Me.cmbID.TabIndex = 1
'
'Label4
'
Me.Label4.Location = New System.Drawing.Point(216, 184)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(168, 16)
Me.Label4.TabIndex = 8
Me.Label4.Text = "(默认密码与用户名相同)"
'
'frmLogin
'
Me.AcceptButton = Me.btnOK
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.BackColor = System.Drawing.Color.FromArgb(CType(224, Byte), CType(224, Byte), CType(224, Byte))
Me.CancelButton = Me.btnCancel
Me.ClientSize = New System.Drawing.Size(432, 275)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.cmbID)
Me.Controls.Add(Me.PictureBox1)
Me.Controls.Add(Me.txtPwd)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.btnCancel)
Me.Controls.Add(Me.btnOK)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.MaximizeBox = False
Me.Name = "frmLogin"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "系统登录"
Me.ResumeLayout(False)
End Sub
#End Region
'保存数据库连接信息
Dim strCon As String = DB.DB.ConnStr
'记录登录用户的权限
Public strLoginRole As String
'记录用户是否成功登录
Public blnLoginSucc As Boolean
Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
'定义四个变量用于检索数据表Users的ID字段
Dim strSql As String = "SELECT ID FROM Users"
Dim myCon As New SqlConnection(strCon)
Dim myCom As New SqlCommand(strSql, myCon)
Dim myReader As SqlDataReader
Dim ErrMsg As String
Try
'打开数据库连接
myCon.Open()
'逐条读取数据表Users记录的ID字段
myReader = myCom.ExecuteReader
'字段非空时将检索值加入到cmbID组合框中
While (myReader.Read)
cmbID.Items.Add(myReader(0))
End While
'操作完成关闭数据库连接
myCon.Close()
Catch ex As Exception
ErrMsg += ex.Message
ErrMsg += vbLf + vbLf + "运行栈信息:" + vbLf + ex.StackTrace
Finally
'显示异常信息并结束程序
If ErrMsg <> "" Then
MsgBox(ErrMsg, MsgBoxStyle.Exclamation + MsgBoxStyle.ApplicationModal + MsgBoxStyle.SystemModal + MsgBoxStyle.MsgBoxSetForeground)
Me.Close()
Application.Exit()
End If
End Try
'有帐户信息时将第一个设置为缺省用户名
If cmbID.Items.Count > 0 Then cmbID.Text = cmbID.Items(0)
'将焦点设置到密码输入框中
txtPwd.Focus()
Me.Activate()
End Sub
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles btnOK.Click
'定义四个变量用于检索数据表Users的Role字段
'检索条件是帐号和密码都符合用户输入
Dim strSql As String = "select Role from users where ID='" & _
cmbID.Text & "' and Pwd='" & txtPwd.Text & "'"
Dim myCon As New SqlConnection(strCon)
Dim myCom As New SqlCommand(strSql, myCon)
Dim myReader As SqlDataReader
Try
'打开数据库连接
myCon.Open()
myReader = myCom.ExecuteReader
'有检索结果说明登录用户合法
If myReader.Read Then
'记录用户权限
strLoginRole = myReader(0)
'设置登陆成功信息
blnLoginSucc = True
'登录成功后退出登录窗体
myCon.Close()
Me.Close()
Else
MsgBox("密码错误,请重新输入!", MsgBoxStyle.Exclamation)
txtPwd.Focus()
Me.DialogResult = DialogResult.None
Return
End If
Catch ex As Exception
'显示异常信息
MsgBox(ex.Message, MsgBoxStyle.Exclamation)
Me.Close()
Application.Exit()
End Try
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles btnCancel.Click
'退出系统
Application.Exit()
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -