一个登录窗体的完整范例,包括登录,密码更改,输入错误三次退出

 

[一个登录窗体的完整范例,包括登录,密码更改,输入错误三次退出] 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 Sub

End 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

        txtNew.Enabled = False

        txtNew2.Enabled = False

        btnOk.Enabled = False

    End Sub

 

    Private m_id As Integer '记录用户ID

    Private m_password As String '记录用户密码(正确的原密码)

    Public Property getid() As Integer

        Get

            Return m_id

        End Get

        Set(ByVal Value As Integer)

            m_id = Value

        End Set

    End Property

    Public Property getpassword() As String

        Get

            Return m_password

        End Get

        Set(ByVal Value As String)

            m_password = Value

        End Set

    End Property

 

    Private Sub btnok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click

        Dim constr As String = "server=localhost;uid=sa;pwd=;database=sheeronerp"

        Dim sqlcon As New SqlClient.SqlConnection()

        Try

            sqlcon.ConnectionString = constr

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

        '定义sql语句,直接进行修改

        Dim str As String = "UPDATE Login SET password='" + txtNew.Text + "'" + " where id=" + CStr(Me.getid)

        Dim cmd As New SqlClient.SqlCommand(str, sqlcon)

        Dim rowcount As Integer

        Try

            sqlcon.Open()

            rowcount = cmd.ExecuteNonQuery

            If rowcount <> 1 Then

                MessageBox.Show("密码更新失败!")

                Exit Sub

            Else

                MessageBox.Show("密码更新成功,请记住您的新密码!")

            End If

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        Finally

            sqlcon.Close()

        End Try

 

    End Sub

 

    Private Sub txtold_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtOld.TextChanged

        If txtOld.Text = Me.getpassword Then

            txtNew.Enabled = True

            txtNew2.Enabled = True

        Else

            txtNew.Enabled = False

            txtNew2.Enabled = False

        End If

    End Sub

 

    Private Sub txtnew2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNew2.TextChanged

        If txtNew2.Text = txtNew.Text Then

            btnOk.Enabled = True

        Else

            btnOk.Enabled = False

        End If

    End Sub

End Class

 

-----------------------------------------------------------------------------------------------

写在结束的话:

[呵呵,我也是一个VB.NET的新手,在学习和使用它的时候遇到了很多困难,在网上找了很多文章来看,得到了不少帮助,现在也来写些文章,希望可以给那些和我一样的初学者以些许帮助。更希望,“高手们”不吝赐教,多多帮忙。如有转载,请注明作者和联系信息。谢谢!

--作者:长春(何秋蔑)

--email:[email protected]

--QQ61106719

--向那些热心帮助过我的相识和不相识的朋友们致敬!   

--2004/12/31

 

你可能感兴趣的:(一个登录窗体的完整范例,包括登录,密码更改,输入错误三次退出)