1. using System;
2. usingSystem.Collections.Generic;
3. using System.ComponentModel;
4. using System.Data;
5. using System.Drawing;
6. using System.Linq;
7. using System.Text;
8. using System.Windows.Forms;
9. usingSystem.Text.RegularExpressions;
10. using System.Data.SqlClient;
11. using System.IO;
12.
13. /*
14. ***************************************************************** *
15. ********************** 登陆窗口源码 ************************ *
16. ********************** By Jason_z ************************ *
17. ********************* QQ:281674669 ************************ *
18. ******************* Email:ccnuzxg@gmail.com ******************** *
19. *********** Blog:http://www.cnblogs.com/Jason_z **************** *
20. ******************* 2009年10月21日 于 杭州国泰**************** *
21. * *****************************************************************
22. */
23. //该源码下载自C#编程网|www.cpbcw.com
24. namespace frmLogin
25. {
26. public partial class Login : Form
27. {
28. public Login()
29. {
30. InitializeComponent();
31. }
32.
33. int count = 0;//记录登陆次数
34.
35. //使用正则表达事匹配
36. private void txtName_KeyPress(object sender,KeyPressEventArgs e)
37. {
38.
39. this.txtName.ImeMode =ImeMode.Off;
40.
41. Regex reg = new Regex(@"^\w+$");
42.
43. if (this.txtName.Text != "")
44. {
45. if (reg.IsMatch(this.txtName.Text))
46. {
47. e.Handled = false;
48.
49. if (File.Exists(@"c:\1.txt"))//路径和文件类型大家自己设置
50. {
51. StreamReader sr = new StreamReader(@"c:\1.txt",true);
52.
53. string str = sr.ReadLine();
54.
55. while (str != null)//判断不为空行
56. {
57. if (!this.txtName.AutoCompleteCustomSource.Contains(str))//是否包含集合里
58. {
59. this.txtName.AutoCompleteCustomSource.Add(str);//不包含添加
60. }
61.
62. str = sr.ReadLine();
63. }
64. sr.Close();
65. }
66. }
67. else
68. {
69. MessageBox.Show("用户名只能为字母、数字和下划线!");
70. //e.Handled= true;
71. }
72. }
73. }
74.
75. /*
76. //通过监听键盘keychar实现
77. privatevoid txtName_KeyPress(object sender, KeyPressEventArgs e)
78. {
79. this.txtName.ImeMode= ImeMode.Off;
80.
81. if((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar >= 'A'&& e.KeyChar <= 'Z') || (e.KeyChar >= 'a' && e.KeyChar<= 'z') || (e.KeyChar == 8) || (e.KeyChar == '_'))
82. {
83. e.Handled= false;
84. }
85.
86. else
87. {
88. MessageBox.Show("用户名只能为字母、数字和下划线!");
89. e.Handled= true;
90. }
91.
92. }
93. */
94.
95. //重置按钮
96. private void btnReset_Click(object sender, EventArgs e)
97. {
98. this.txtName.Text = "";
99. this.txtPassword.Text = "";
100. }
101.
102. //取消按钮
103. private void btnCancle_Click(object sender, EventArgs e)
104. {
105. this.Close();
106. }
107.
108. //登陆按钮
109. private void btnLogin_Click(object sender, EventArgs e)
110. {
111. if(count<3)//首先判断登陆次数
112. {
113. if (this.txtName.Text == "")
114. {
115. MessageBox.Show("用户名不能为空!");
116. }
117. else
118. {
119. SqlConnection SqlCon = new SqlConnection(@"server=(local);database=UserInfo;UserId=sa;pwd=1234;Pooling=True;");//连接数据库
120.
121. SqlCon.Open();//打开连接
122.
123. SqlCommand Cmd = new SqlCommand("ProcUser", SqlCon);//调用存储过程
124.
125. Cmd.CommandType =CommandType.StoredProcedure;//设置解释命令为存储过程
126. SqlParameter p = Cmd.Parameters.Add("@N", SqlDbType.VarChar, 20);//设置存储过程需要的参数"@N"
127.
128. p.Value = this.txtName.Text;//给"@N"赋值
129.
130. p = Cmd.Parameters.Add("@P", SqlDbType.VarChar, 20);//设置存储过程需要的参数"@P"
131. p.Value = this.txtPassword.Text;//给"@P"赋值
132.
133. SqlDataReader Reader = Cmd.ExecuteReader();//执行
134.
135. Reader.Read();
136.
137. if (Reader.HasRows)//判断是否有查询到对象的数据
138. {
139. if (File.Exists(@"c:\1.txt"))//判断文件存在
140. {
141. if (!this.txtName.AutoCompleteCustomSource.Contains(this.txtName.Text))//判断记录是否存在
142. {
143. StreamWriter sw = new StreamWriter(@"c:\1.txt", true);//true参数不可少,否则会覆盖以前存入的记录
144.
145. sw.WriteLine(this.txtName.Text.Trim());//存入记录
146.
147. sw.Close();//关闭文件流
148.
149. if (!this.txtName.AutoCompleteCustomSource.Contains(this.txtName.Text))
150. {
151. this.txtName.AutoCompleteCustomSource.Add(this.txtName.Text);//Downloadcpbcw.com
152. }
153. }
154. }
155.
156. this.DialogResult =DialogResult.OK;
157. }
158.
159. else
160. {
161. MessageBox.Show("用户名或密码错误","提示");
162. count++;
163. }
164.
165. SqlCon.Close();//关闭连接
166. }
167.
168. }
169. else
170. {
171. MessageBox.Show("超过系统登陆最大次数", "警告", MessageBoxButtons.OK,MessageBoxIcon.Warning);//cpbcw.com
172. this.Close();
173.
174. }
175.
176. }
177.
178. }
179.
180. }