1.用户名为空时,文本框变颜色
2.进入到用户名后,恢复为原来颜色
3.当用户名超过4位时,登录按钮不可用,并且提示
4.双击用户名文本框,登录按钮可用
5.当用户名超过6位时,弹出错误提示
6.按回车进入下一个文本框
7.关闭窗体时,弹出是否关闭的对话框
8.按ENter=按登录,按Esc=按退出
9.防止窗体在任务栏出现
10.去掉最大化或最小化按钮
11.默认最大化的显示窗体
12.设置显示窗体中屏幕的中间
13.鼠标经过用户名文本框附近出现提示语
14.修改登录按钮为圆形
15.简单的验证码
16.填写的验证码必须是数字的验证
17.设置窗体与背景图片一样大小
18.防止用户使用Alt+F4关闭窗体
19.运行后,无法拖放窗体,两种方法
Code
1 using System;
2 using System.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 using System.Collections;
10 using System.Drawing.Drawing2D;
11 using System.Text.RegularExpressions;
12
13 namespace 窗体练习
14 {
15 /*********************************
16 * 1.用户名为空时,文本框变颜色
17 * 2.进入到用户名后,恢复为原来颜色
18 * 3.当用户名超过4位时,登录按钮不可用,并且提示
19 * 4.双击用户名文本框,登录按钮可用
20 * 5.当用户名超过6位时,弹出错误提示
21 * 6.按回车进入下一个文本框
22 * 7.关闭窗体时,弹出是否关闭的对话框
23 * 8.按ENter=按登录,按Esc=按退出
24 * 9.防止窗体在任务栏出现
25 * 10.去掉最大化或最小化按钮
26 * 11.默认最大化的显示窗体
27 * 12.设置显示窗体中屏幕的中间
28 * 13.鼠标经过用户名文本框附近出现提示语
29 * 14.修改登录按钮为圆形
30 * 15.简单的验证码
31 * 16.填写的验证码必须是数字的验证
32 * 17.设置窗体与背景图片一样大小
33 * 18.防止用户使用Alt+F4关闭窗体
34 * 19.运行后,无法拖放窗体,两种方法
35 */
36 public partial class 登录 : Form
37 {
38 public 登录()
39 {
40 InitializeComponent();
41 this.AcceptButton = button1;//8
42 this.CancelButton = button3;
43 this.ShowInTaskbar = false;//9
44 this.MaximizeBox = false;//10
45 //this.WindowState = FormWindowState.Maximized;//11
46 this.StartPosition = FormStartPosition.CenterScreen;//12
47 //this.FormBorderStyle = FormBorderStyle.FixedSingle;//19-1
48 //this.AutoSize = false;//19-2-1
49 //this.AutoSizeMode = AutoSizeMode.GrowAndShrink;//19-2-2
50 }
51 //登录判断
52 private void button1_Click(object sender, EventArgs e)
53 {
54 if (textBox1.Text == "a")
55 {
56 if (textBox2.Text == "a"&&textBox4.Text==valid)
57 {
58 button4.Enabled = true;
59 }
60 else
61 {
62 MessageBox.Show("密码错误");
63 textBox2.Text = string.Empty;
64 }
65 }
66 else
67 {
68 if (textBox2.Text == "a")
69 {
70 MessageBox.Show("用户名错误");
71 textBox1.Text = string.Empty;
72 }
73 else
74 {
75 MessageBox.Show("用户名和密码错误");
76 textBox1.Text = string.Empty;
77 textBox2.Text = string.Empty;
78 textBox1.Focus();
79 }
80 }
81
82 }
83 //清空也可以使用string.Empty
84 private void button2_Click(object sender, EventArgs e)
85 {
86 textBox2.Text = "";
87 textBox1.Text = "";
88 }
89 //退出程序
90 private void button3_Click(object sender, EventArgs e)
91 {
92 /*当本窗口是主线程窗口,以下三种方法均可以退出程序
93 *当本窗口不是主窗口时,如果要退出整个程序,应采用第3种方法,将一次性关闭程序上存在的所有线程。
94 *如果只是想关闭当前窗口而不退出程序,应采用第一种方法
95 *当本窗口不是主窗口时,采用第2种方法退出程序时,将会再执行关闭主窗体的步骤。
96
97 */
98 //this.Close();
99 //Application.Exit();
100 Application.ExitThread();
101
102 }
103 //弹出新的窗体
104 private void button4_Click(object sender, EventArgs e)
105 {
106 通讯录 f = new 通讯录();
107 f.Show();
108 }
109
110 //3
111 private void textBox1_TextChanged(object sender, EventArgs e)
112 {
113 if (textBox1.Text.Length > 4)
114 button1.Enabled = false;
115 }
116 //3
117 private void button1_EnabledChanged(object sender, EventArgs e)
118 {
119 if (!button1.Enabled)
120 label3.Text = "请双击文本框重新登录!";
121 }
122 //4
123 private void textBox1_DoubleClick(object sender, EventArgs e)
124 {
125 textBox1.Text = "";
126 label3.Visible = false;
127 button1.Enabled = true;
128 }
129 //5
130 private void textBox1_Leave(object sender, EventArgs e)
131 {
132 if (textBox1.Text.Length > 6)
133 {
134 MessageBox.Show("用户名超过6位");
135 textBox1.Text = "";
136 textBox1.Focus();
137 }
138 //1
139 else if (textBox1.Text.Length == 0)
140 textBox1.BackColor = Color.Yellow;
141 }
142
143
144 private void 登录_Load(object sender, EventArgs e)
145 {
146 //13
147 toolTip1.SetToolTip(textBox3,"这里显示网址!");
148 //14
149 GraphicsPath path = new GraphicsPath();
150 path.AddEllipse(this.button1.ClientRectangle);
151 button1.Region = new Region(path);
152
153 //产生验证码
154 GetValid();
155 label6.Font =new Font ("宋体",9,FontStyle.Italic);
156 label6.BackColor = Color.Transparent;
157
158 ////17.设置窗体工作区的大小
159 //this.ClientSize = this.BackgroundImage.Size;
160
161 CodeImage(GetcheckCode());
162
163
164 }
165 //2
166 private void textBox1_Enter(object sender, EventArgs e)
167 {
168 //textBox1.BackColor = textBox2.BackColor;
169 textBox1.BackColor = Color.FromKnownColor(KnownColor.Window);
170 }
171 //6
172 private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
173 {
174 if (e.KeyChar == 13)
175 textBox2.Focus();
176 }
177 //7
178 private void 登录_FormClosing(object sender, FormClosingEventArgs e)
179 {
180 DialogResult jg = MessageBox.Show("你确定要关闭吗?", "关闭登录窗体", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
181 if (jg == DialogResult.No)
182 e.Cancel = true;
183 }
184
185 private void button5_Click(object sender, EventArgs e)
186 {
187 webBrowser1.Navigate(textBox3.Text.Trim());
188 }
189 //13
190 private void textBox2_MouseEnter(object sender, EventArgs e)
191 {
192 toolTip2.Show("请输入密码!",this);
193 }
194 //15产生4个验证码
195 string valid;
196 void GetValid()
197 {
198 Random rad = new Random();
199
200 for (int i = 0; i < 4; i++)
201 valid += rad.Next(0,9).ToString ();
202 label6.Text = valid;
203 }
204 //16.验证是数字的方法
205 bool IsNum(string s)
206 {
207 //1.使用异常检测的方法
208 //try
209 //{
210 // Convert .ToInt32(s);
211 //}
212 //catch
213 //{
214 // return false ;
215 //}
216 //return true ;
217
218 ////2.使用正则表达式
219 //string regex = @"^\d+(\.\d)?$";
220 //if (!Regex.IsMatch(s, regex))
221 // return false;
222 //else
223 // return true;
224
225 //3.调用VB的information的方法,判断是否为数字。先添加Microsoft.VisualBasic引用
226 return Microsoft.VisualBasic.Information.IsNumeric(s);
227
228 }
229 //16
230 private void textBox4_Leave(object sender, EventArgs e)
231 {
232 if (!IsNum(textBox4.Text)||textBox4.Text .Trim ().Length!=4)
233 {
234 textBox4.Text = "";
235 MessageBox.Show("请输入正确的数字格式!");
236 }
237 }
238 //18
239 private void 登录_KeyDown(object sender, KeyEventArgs e)
240 {
241 if (e.KeyCode == Keys.F4 && e.Modifiers == Keys.Alt)
242 e.Handled = true;
243 }
244 //产生随机数
245 string GetcheckCode()
246 {
247 int number;
248 char code;
249 string checkcode = "";
250 Random rdm = new Random();
251 for (int i = 0; i < 4; i++)
252 {
253 number = rdm.Next();
254 //number = rdm.Next(0,9);
255 if (number % 2 == 0)
256 code = (char)('0' + (char)(number % 10));
257 else
258 code = (char)('A' + (char)(number % 26));
259 checkcode +="" +code.ToString ();
260 }
261 return checkcode;
262 }
263 void CodeImage(string code)
264 {
265
266 if (code == null || code.Trim() == string.Empty)
267 return;
268 System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling (code.Length*10.0),22);
269 Graphics g = Graphics.FromImage(image);
270 try
271 {
272 Random rdm = new Random();
273 g.Clear(Color.White);
274 //画图片的背景噪音线
275 for (int i = 0; i < 3; i++)
276 {
277 int x1 = rdm.Next(image.Width);
278 int x2 = rdm.Next(image.Width);
279 int y1 = rdm.Next(image.Height);
280 int y2 = rdm.Next(image.Height);
281 g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
282 }
283 Font font = new System.Drawing.Font("Arial", 12, FontStyle.Bold);
284 g.DrawString(code, font, new SolidBrush(Color.Red), 2, 2);
285 //画前景噪音线
286 for (int i = 0; i < 150; i++)
287 {
288 int x = rdm.Next(image.Width);
289 int y = rdm.Next(image.Height);
290 image.SetPixel(x, y, Color.FromArgb(rdm.Next()));
291 }
292 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
293 pictureBox1.Width = image.Width;
294 pictureBox1.Height = image.Height;
295 pictureBox1.Image = image;
296 }
297 catch
298 { }
299 }
300 }
301 }
302
1 using System;
2 using System.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 using System.Collections;
10 using System.Drawing.Drawing2D;
11 using System.Text.RegularExpressions;
12
13 namespace 窗体练习
14 {
15 /*********************************
16 * 1.用户名为空时,文本框变颜色
17 * 2.进入到用户名后,恢复为原来颜色
18 * 3.当用户名超过4位时,登录按钮不可用,并且提示
19 * 4.双击用户名文本框,登录按钮可用
20 * 5.当用户名超过6位时,弹出错误提示
21 * 6.按回车进入下一个文本框
22 * 7.关闭窗体时,弹出是否关闭的对话框
23 * 8.按ENter=按登录,按Esc=按退出
24 * 9.防止窗体在任务栏出现
25 * 10.去掉最大化或最小化按钮
26 * 11.默认最大化的显示窗体
27 * 12.设置显示窗体中屏幕的中间
28 * 13.鼠标经过用户名文本框附近出现提示语
29 * 14.修改登录按钮为圆形
30 * 15.简单的验证码
31 * 16.填写的验证码必须是数字的验证
32 * 17.设置窗体与背景图片一样大小
33 * 18.防止用户使用Alt+F4关闭窗体
34 * 19.运行后,无法拖放窗体,两种方法
35 */
36 public partial class 登录 : Form
37 {
38 public 登录()
39 {
40 InitializeComponent();
41 this.AcceptButton = button1;//8
42 this.CancelButton = button3;
43 this.ShowInTaskbar = false;//9
44 this.MaximizeBox = false;//10
45 //this.WindowState = FormWindowState.Maximized;//11
46 this.StartPosition = FormStartPosition.CenterScreen;//12
47 //this.FormBorderStyle = FormBorderStyle.FixedSingle;//19-1
48 //this.AutoSize = false;//19-2-1
49 //this.AutoSizeMode = AutoSizeMode.GrowAndShrink;//19-2-2
50 }
51 //登录判断
52 private void button1_Click(object sender, EventArgs e)
53 {
54 if (textBox1.Text == "a")
55 {
56 if (textBox2.Text == "a"&&textBox4.Text==valid)
57 {
58 button4.Enabled = true;
59 }
60 else
61 {
62 MessageBox.Show("密码错误");
63 textBox2.Text = string.Empty;
64 }
65 }
66 else
67 {
68 if (textBox2.Text == "a")
69 {
70 MessageBox.Show("用户名错误");
71 textBox1.Text = string.Empty;
72 }
73 else
74 {
75 MessageBox.Show("用户名和密码错误");
76 textBox1.Text = string.Empty;
77 textBox2.Text = string.Empty;
78 textBox1.Focus();
79 }
80 }
81
82 }
83 //清空也可以使用string.Empty
84 private void button2_Click(object sender, EventArgs e)
85 {
86 textBox2.Text = "";
87 textBox1.Text = "";
88 }
89 //退出程序
90 private void button3_Click(object sender, EventArgs e)
91 {
92 /*当本窗口是主线程窗口,以下三种方法均可以退出程序
93 *当本窗口不是主窗口时,如果要退出整个程序,应采用第3种方法,将一次性关闭程序上存在的所有线程。
94 *如果只是想关闭当前窗口而不退出程序,应采用第一种方法
95 *当本窗口不是主窗口时,采用第2种方法退出程序时,将会再执行关闭主窗体的步骤。
96
97 */
98 //this.Close();
99 //Application.Exit();
100 Application.ExitThread();
101
102 }
103 //弹出新的窗体
104 private void button4_Click(object sender, EventArgs e)
105 {
106 通讯录 f = new 通讯录();
107 f.Show();
108 }
109
110 //3
111 private void textBox1_TextChanged(object sender, EventArgs e)
112 {
113 if (textBox1.Text.Length > 4)
114 button1.Enabled = false;
115 }
116 //3
117 private void button1_EnabledChanged(object sender, EventArgs e)
118 {
119 if (!button1.Enabled)
120 label3.Text = "请双击文本框重新登录!";
121 }
122 //4
123 private void textBox1_DoubleClick(object sender, EventArgs e)
124 {
125 textBox1.Text = "";
126 label3.Visible = false;
127 button1.Enabled = true;
128 }
129 //5
130 private void textBox1_Leave(object sender, EventArgs e)
131 {
132 if (textBox1.Text.Length > 6)
133 {
134 MessageBox.Show("用户名超过6位");
135 textBox1.Text = "";
136 textBox1.Focus();
137 }
138 //1
139 else if (textBox1.Text.Length == 0)
140 textBox1.BackColor = Color.Yellow;
141 }
142
143
144 private void 登录_Load(object sender, EventArgs e)
145 {
146 //13
147 toolTip1.SetToolTip(textBox3,"这里显示网址!");
148 //14
149 GraphicsPath path = new GraphicsPath();
150 path.AddEllipse(this.button1.ClientRectangle);
151 button1.Region = new Region(path);
152
153 //产生验证码
154 GetValid();
155 label6.Font =new Font ("宋体",9,FontStyle.Italic);
156 label6.BackColor = Color.Transparent;
157
158 ////17.设置窗体工作区的大小
159 //this.ClientSize = this.BackgroundImage.Size;
160
161 CodeImage(GetcheckCode());
162
163
164 }
165 //2
166 private void textBox1_Enter(object sender, EventArgs e)
167 {
168 //textBox1.BackColor = textBox2.BackColor;
169 textBox1.BackColor = Color.FromKnownColor(KnownColor.Window);
170 }
171 //6
172 private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
173 {
174 if (e.KeyChar == 13)
175 textBox2.Focus();
176 }
177 //7
178 private void 登录_FormClosing(object sender, FormClosingEventArgs e)
179 {
180 DialogResult jg = MessageBox.Show("你确定要关闭吗?", "关闭登录窗体", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
181 if (jg == DialogResult.No)
182 e.Cancel = true;
183 }
184
185 private void button5_Click(object sender, EventArgs e)
186 {
187 webBrowser1.Navigate(textBox3.Text.Trim());
188 }
189 //13
190 private void textBox2_MouseEnter(object sender, EventArgs e)
191 {
192 toolTip2.Show("请输入密码!",this);
193 }
194 //15产生4个验证码
195 string valid;
196 void GetValid()
197 {
198 Random rad = new Random();
199
200 for (int i = 0; i < 4; i++)
201 valid += rad.Next(0,9).ToString ();
202 label6.Text = valid;
203 }
204 //16.验证是数字的方法
205 bool IsNum(string s)
206 {
207 //1.使用异常检测的方法
208 //try
209 //{
210 // Convert .ToInt32(s);
211 //}
212 //catch
213 //{
214 // return false ;
215 //}
216 //return true ;
217
218 ////2.使用正则表达式
219 //string regex = @"^\d+(\.\d)?$";
220 //if (!Regex.IsMatch(s, regex))
221 // return false;
222 //else
223 // return true;
224
225 //3.调用VB的information的方法,判断是否为数字。先添加Microsoft.VisualBasic引用
226 return Microsoft.VisualBasic.Information.IsNumeric(s);
227
228 }
229 //16
230 private void textBox4_Leave(object sender, EventArgs e)
231 {
232 if (!IsNum(textBox4.Text)||textBox4.Text .Trim ().Length!=4)
233 {
234 textBox4.Text = "";
235 MessageBox.Show("请输入正确的数字格式!");
236 }
237 }
238 //18
239 private void 登录_KeyDown(object sender, KeyEventArgs e)
240 {
241 if (e.KeyCode == Keys.F4 && e.Modifiers == Keys.Alt)
242 e.Handled = true;
243 }
244 //产生随机数
245 string GetcheckCode()
246 {
247 int number;
248 char code;
249 string checkcode = "";
250 Random rdm = new Random();
251 for (int i = 0; i < 4; i++)
252 {
253 number = rdm.Next();
254 //number = rdm.Next(0,9);
255 if (number % 2 == 0)
256 code = (char)('0' + (char)(number % 10));
257 else
258 code = (char)('A' + (char)(number % 26));
259 checkcode +="" +code.ToString ();
260 }
261 return checkcode;
262 }
263 void CodeImage(string code)
264 {
265
266 if (code == null || code.Trim() == string.Empty)
267 return;
268 System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling (code.Length*10.0),22);
269 Graphics g = Graphics.FromImage(image);
270 try
271 {
272 Random rdm = new Random();
273 g.Clear(Color.White);
274 //画图片的背景噪音线
275 for (int i = 0; i < 3; i++)
276 {
277 int x1 = rdm.Next(image.Width);
278 int x2 = rdm.Next(image.Width);
279 int y1 = rdm.Next(image.Height);
280 int y2 = rdm.Next(image.Height);
281 g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
282 }
283 Font font = new System.Drawing.Font("Arial", 12, FontStyle.Bold);
284 g.DrawString(code, font, new SolidBrush(Color.Red), 2, 2);
285 //画前景噪音线
286 for (int i = 0; i < 150; i++)
287 {
288 int x = rdm.Next(image.Width);
289 int y = rdm.Next(image.Height);
290 image.SetPixel(x, y, Color.FromArgb(rdm.Next()));
291 }
292 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
293 pictureBox1.Width = image.Width;
294 pictureBox1.Height = image.Height;
295 pictureBox1.Image = image;
296 }
297 catch
298 { }
299 }
300 }
301 }
302