【 tulaoshi.com - ASP.NET 】
[一个登录窗体的完整范例,包括登录,密码更改,输入错误三次退出] 2004-12-31 运行效果如下:(请参考设置窗体布局)
(登录主窗体效果) (修改密码窗体效果)控件名称说明:‘cboUser就是图中的combobox控件 Public Class frmLogin Inherits System.Windows.Forms.Form Public Sub New() MyBase.New() '该调用是 Windows 窗体设计器所必需的。 InitializeComponent() '在 InitializeComponent() 调用之后添加任何初始化 ' 填充数据,并定义datatable的主键 ‘ 请根据情况选择连接方式和数据库类型 Dim constr As String = "server=localhost;uid=sa;pwd=;database=sheeronerp" Dim str As String = "SELECT id,name,password,power FROM Login ORDER BY name" Dim sqlcon As New SqlClient.SqlConnection() Dim sqldpr As New SqlClient.SqlDataAdapter(str, sqlcon) Try sqlcon.ConnectionString = constr sqlcon.Open() ‘其实没有sqlcon.open()和sqlcon.close()语句也没关系,适配器会自动进行 sqldpr.Fill(table) Catch ex As Exception MessageBox.Show(ex.Message) Finally sqlcon.Close() End Try ‘设置主键,目的是使用find()方法 table.PrimaryKey = New DataColumn() {table.Columns("id")} Me.cboUser.DataSource = table Me.cboUser.DisplayMember = "name" Me.cboUser.ValueMember = "id" '初始时不选中任何项 Me.cboUser.SelectedIndex = -1 Me.cboUser.Focus() End Sub '窗体重写处置以清理组件列表。 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 Dim count As Integer = 0 '登录出错记数器,登录时输入3次均错误则自动退出 Dim table As New DataTable()‘确定按钮代码 Private Sub btnok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnok.Click If Me.cboUser.Text = "" Then MessageBox.Show("请选择用户名称!") cboUser.Focus() Return End If If Me.txtPwd.Text = "" Then MessageBox.Show("请输入用户密码!") txtPwd.Focus() Exit Sub End If '获取combobox 中选择的用户id,检索这条记录 Dim getrows As DataRow = table.Rows.Find(cboUser.SelectedValue) If Not (getrows Is Nothing) Then Dim password As String = Trim(getrows("password")) If password.Trim(" ") = txtPwd.Text.Trim(" ") Then UserId = cboUser.SelectedIndex UserName = cboUser.Text UserPower = getrows.Item("power") Me.Dispose() '??????????????????????? '显示主窗体 Dim newform1 As New frmorder() newform1.ShowDialog() Else '输入不正确的话,则判断输入次数,3次错误则退出 If count = 2 Then MessageBox.Show("密码错误输入3次,即将退出系统!") End Else MessageBox.Show("密码有误,请重新输入!") count = count + 1 Me.txtPwd.Focus() Me.txtPwd.SelectAll() Return End If End If End If End Sub‘取消按钮代码 Private Sub btncancle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncancle.Click End End Sub ‘更改密码按钮代码 Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click If Me.cboUser.Text = "" Then MessageBox.Show("请输入或选择登录帐户!") cboUser.Focus() Return End If Dim findrows As DataRow findrows = table.Rows.Find(cboUser.SelectedValue) '获取所选id对应的密码 Dim strpassword As String = Trim(findrows("password")) '定义窗体实例,并传递参数过去 Dim formnew As New frmUpdatePWD(cboUser.SelectedValue, strpassword) formnew.ShowDialog() End Sub Private Sub cboUser_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cboUser.KeyPress‘按下回车键响应TAB键操作 If e.KeyChar = Chr(13) Then e.Handled = True SendKeys.Send("{TAB}") End If End SubEnd Class ‘----------------------------------------------------------------------------Public Class frmUpdatePWD Inherits System.Windows.Forms.Form ' 重载构造函数 主要是为了传递过来用户ID和密码两个属性 Public Sub New(ByVal userid As Integer, ByVal password As String) MyBase.New() '该调用是 Windows 窗体设计器所必需的。 InitializeComponent() '在 InitializeComponent() 调用之后添加任何初始化 Me.m_id = userid Me.m_password = password txtNe