您的位置:首页 > 其它

源码pub:功能强大的登陆窗体(winForm)

2009-10-22 09:53 183 查看
抱歉,昨天晚上没来得及整理,没有将源码及时发上来

注:本程序完全可以当作Demo,有心的朋友,也可以将用户名和密码两个文本框制作成自己的控件,方便以后使用,初学者,我建议下载源程序后对照我的文章《一个登陆窗体引发的问题系列》进行学习。

程序功能:

1.对用户名进行了输入限制,仅能输入数字、字母和下划线的组合,且长度不超过10个字符

2.对密码进行了限制,屏蔽了鼠标右键,禁止粘贴和复制,且长度不超过16个字符

3.对登陆次数进行了限制

4.使用存储过程建立数据连接,提高了系统的性能和安全性

5.良好的窗口跳转,防止了不经过验证也能进入主界面的错误。

6.用户名输入采用了记忆功能,提高了软件的交互性

程序存在的部分bug:

1.用户名可以进行粘贴(其实影响不大)

2.用户名输入的时候,在输入第二个字符的时候会出现轻微的闪屏

3.登陆的时候有时会响应较慢

更多程序Bug,请在博客留言,或者邮件到ccnuzxg@gmail.com

本程序源码及数据库下载地址:

http://files.cnblogs.com/Jason_z/登陆窗体源码.rar

Login.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.IO;

/*
* **************************************************************** *
* ********************* 登陆窗口源码 ************************ *
* ********************* By Jason_z ************************ *
* ******************** QQ:281674669 ************************ *
* ****************** Email:ccnuzxg@gmail.com ******************** *
* ********** Blog:http://www.cnblogs.com/Jason_z **************** *
* ****************** 2009年10月21日 于 杭州国泰 **************** *
* **************************************************************** *
*/

namespace frmLogin
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}

int count = 0;//记录登陆次数

//使用正则表达事匹配
private void txtName_KeyPress(object sender, KeyPressEventArgs e)
{

this.txtName.ImeMode = ImeMode.Off;

Regex reg = new Regex(@"^\w+$");

if (this.txtName.Text != "")
{
if (reg.IsMatch(this.txtName.Text))
{
e.Handled = false;

if (File.Exists(@"c:\1.txt"))//路径和文件类型大家自己设置
{
StreamReader sr = new StreamReader(@"c:\1.txt",true);

string str = sr.ReadLine();

while (str != null)//判断不为空行
{
if (!this.txtName.AutoCompleteCustomSource.Contains(str))//是否包含集合里
{
this.txtName.AutoCompleteCustomSource.Add(str);//不包含添加
}

str = sr.ReadLine();
}
sr.Close();
}
}
else
{
MessageBox.Show("用户名只能为字母、数字和下划线!");
//e.Handled = true;
}
}
}

/*
//通过监听键盘keychar实现
private void txtName_KeyPress(object sender, KeyPressEventArgs e)
{
this.txtName.ImeMode = ImeMode.Off;

if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z') || (e.KeyChar >= 'a' && e.KeyChar <= 'z') || (e.KeyChar == 8) || (e.KeyChar == '_'))
{
e.Handled = false;
}

else
{
MessageBox.Show("用户名只能为字母、数字和下划线!");
e.Handled = true;
}

}
*/

//重置按钮
private void btnReset_Click(object sender, EventArgs e)
{
this.txtName.Text = "";
this.txtPassword.Text = "";
}

//取消按钮
private void btnCancle_Click(object sender, EventArgs e)
{
this.Close();
}

//登陆按钮
private void btnLogin_Click(object sender, EventArgs e)
{
if(count<5)//首先判断登陆次数
{
if (this.txtName.Text == "")
{
MessageBox.Show("用户名不能为空!");
}
else
{
SqlConnection SqlCon = new SqlConnection(@"server=zhouw;database=UserInfo;Trusted_Connection=SSPI");//连接数据库

SqlCon.Open();//打开连接

SqlCommand Cmd = new SqlCommand("ProcUser", SqlCon);//调用存储过程

Cmd.CommandType = CommandType.StoredProcedure;//设置解释命令为存储过程
SqlParameter p = Cmd.Parameters.Add("@N", SqlDbType.VarChar, 20);//设置存储过程需要的参数"@N"

p.Value = this.txtName.Text;//给"@N"赋值

p = Cmd.Parameters.Add("@P", SqlDbType.VarChar, 20);//设置存储过程需要的参数"@P"
p.Value = this.txtPassword.Text;//给"@P"赋值

SqlDataReader Reader = Cmd.ExecuteReader();//执行

Reader.Read();

if (Reader.HasRows)//判断是否有查询到对象的数据
{
if (File.Exists(@"c:\1.txt"))//判断文件存在
{
if (!this.txtName.AutoCompleteCustomSource.Contains(this.txtName.Text))//判断记录是否存在
{
StreamWriter sw = new StreamWriter(@"c:\1.txt", true);//true参数不可少,否则会覆盖以前存入的记录

sw.WriteLine(this.txtName.Text.Trim());//存入记录

sw.Close();//关闭文件流

if (!this.txtName.AutoCompleteCustomSource.Contains(this.txtName.Text))
{
this.txtName.AutoCompleteCustomSource.Add(this.txtName.Text);
}
}
}

this.DialogResult = DialogResult.OK;
}

else
{
MessageBox.Show("用户名或密码错误","提示");
count++;
}

SqlCon.Close();//关闭连接
}

}
else
{
MessageBox.Show("超过系统登陆最大次数","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
this.Close();

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: