C#——WinForm登录界面

来源:互联网 发布:曾舜晞粤读软件 编辑:程序博客网 时间:2024/05/16 15:27

登录界面概述:
系统登录主要用于对用户进行安全性检查,以防止非法用户进入系统。在登录时,只有合法的用户才可以进入该系统。

登录界面技术分析:
登录模块的重点在于将用户输入的用户名和密码与数据库中的用户名和密码进行比较,如果相同则允许用户进入系统的操作界面;否则弹出提示框,提示用户输入的用户名或密码错误。该模块的实现原理是,根据用户输入的用户名和密码在数据库中查找是否有相符的记录,并将查询结果填充到DataSet数据集中,然后判断该数据集中所包含表的行数是否大于零,如果大于零,则表示输入的用户和密码正确,从而成功登陆系统;否则,弹出提示信息。

窗体控件
这里写图片描述

窗体界面
这里写图片描述
代码
登陆按钮

private void btnLogin_Click(object sender, EventArgs e)        {            if (txtName.Text == "")            {                errorProName.SetError(txtName, "用户名不能为空!");            }            else            {                errorProName.Clear();                string strSql = "select * from tb_admin where name='" + txtName.Text + "' and pwd='" + txtPwd.Text + "'";                DataSet ds = dataoperate.getDs(strSql, "tb_admin");                if (ds.Tables[0].Rows.Count > 0)                {                    this.Hide();                    frmMain frmmain = new frmMain();                    frmmain.Show();                }                else                {                    MessageBox.Show("用户名或密码错误!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);                }            }        }

退出按钮

 private void btnExit_Click(object sender, EventArgs e)        {            Application.Exit();        }

用户名输入

private void txtName_KeyPress(object sender, KeyPressEventArgs e)        {            if (e.KeyChar == 13)            {                txtPwd.Focus();                e.Handled = true;            }        }

密码输入

  private void txtPwd_KeyPress(object sender, KeyPressEventArgs e)        {            if (e.KeyChar == 13)            {                btnLogin.Focus();                e.Handled = true;            }        }

程序:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;//添加的命名空间using System.Data.OleDb;using VWMS.CommonClass;namespace VWMS{    public partial class frmLogin : Form    {        public frmLogin()        {            InitializeComponent();        }        DataCon datacon = new DataCon();        DataOperate dataoperate = new DataOperate();        private void btnLogin_Click(object sender, EventArgs e)        {            if (txtName.Text == "")            {                errorProName.SetError(txtName, "用户名不能为空!");            }            else            {                errorProName.Clear();                string strSql = "select * from tb_admin where name='" + txtName.Text + "' and pwd='" + txtPwd.Text + "'";                DataSet ds = dataoperate.getDs(strSql, "tb_admin");                if (ds.Tables[0].Rows.Count > 0)                {                    this.Hide();                    frmMain frmmain = new frmMain();                    frmmain.Show();                }                else                {                    MessageBox.Show("用户名或密码错误!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);                }            }        }        private void btnExit_Click(object sender, EventArgs e)        {            Application.Exit();        }        private void txtName_KeyPress(object sender, KeyPressEventArgs e)        {            if (e.KeyChar == 13)            {                txtPwd.Focus();                e.Handled = true;            }        }        private void txtPwd_KeyPress(object sender, KeyPressEventArgs e)        {            if (e.KeyChar == 13)            {                btnLogin.Focus();                e.Handled = true;            }        }    }}
0 1