1.用户名为空时,文本框变颜色
2.进入到用户名后,恢复为原来颜色
3.当用户名超过4位时,登录按钮不可用,并且提示
4.双击用户名文本框,登录按钮可用
5.当用户名超过6位时,弹出错误提示
6.按回车进入下一个文本框
7.关闭窗体时,弹出是否关闭的对话框
8.按ENter=按登录,按Esc=按退出
9.防止窗体在任务栏出现
10.去掉最大化或最小化按钮
11.默认最大化的显示窗体
12.设置显示窗体中屏幕的中间
13.鼠标经过用户名文本框附近出现提示语
14.修改登录按钮为圆形
15.简单的验证码
16.填写的验证码必须是数字的验证
17.设置窗体与背景图片一样大小
2.进入到用户名后,恢复为原来颜色
3.当用户名超过4位时,登录按钮不可用,并且提示
4.双击用户名文本框,登录按钮可用
5.当用户名超过6位时,弹出错误提示
6.按回车进入下一个文本框
7.关闭窗体时,弹出是否关闭的对话框
8.按ENter=按登录,按Esc=按退出
9.防止窗体在任务栏出现
10.去掉最大化或最小化按钮
11.默认最大化的显示窗体
12.设置显示窗体中屏幕的中间
13.鼠标经过用户名文本框附近出现提示语
14.修改登录按钮为圆形
15.简单的验证码
16.填写的验证码必须是数字的验证
17.设置窗体与背景图片一样大小
18.防止用户使用Alt+F4关闭窗体
19.运行后,无法拖放窗体,两种方法
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.Collections;
using System.Drawing.Drawing2D;
using System.Text.RegularExpressions;
namespace 窗体练习
{
/*********************************
* 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.运行后,无法拖放窗体,两种方法
*/
public partial class 登录 : Form
{
public 登录()
{
InitializeComponent();
this.AcceptButton = button1; //8
this.CancelButton = button3;
this.ShowInTaskbar = false; //9
this.MaximizeBox = false; //10
//this.WindowState = FormWindowState.Maximized;//11
this.StartPosition = FormStartPosition.CenterScreen; //12
//this.FormBorderStyle = FormBorderStyle.FixedSingle;//19-1
//this.AutoSize = false;//19-2-1
//this.AutoSizeMode = AutoSizeMode.GrowAndShrink;//19-2-2
}
//登录判断
private void button1_Click( object sender, EventArgs e)
{
if (textBox1.Text == "a")
{
if (textBox2.Text == "a"&&textBox4.Text==valid)
{
button4.Enabled = true;
}
else
{
MessageBox.Show( "密码错误");
textBox2.Text = string.Empty;
}
}
else
{
if (textBox2.Text == "a")
{
MessageBox.Show( "用户名错误");
textBox1.Text = string.Empty;
}
else
{
MessageBox.Show( "用户名和密码错误");
textBox1.Text = string.Empty;
textBox2.Text = string.Empty;
textBox1.Focus();
}
}
}
//清空也可以使用string.Empty
private void button2_Click( object sender, EventArgs e)
{
textBox2.Text = "";
textBox1.Text = "";
}
//退出程序
private void button3_Click( object sender, EventArgs e)
{
/*当本窗口是主线程窗口,以下三种方法均可以退出程序
*当本窗口不是主窗口时,如果要退出整个程序,应采用第3种方法,将一次性关闭程序上存在的所有线程。
*如果只是想关闭当前窗口而不退出程序,应采用第一种方法
*当本窗口不是主窗口时,采用第2种方法退出程序时,将会再执行关闭主窗体的步骤。
*/
//this.Close();
//Application.Exit();
Application.ExitThread();
}
//弹出新的窗体
private void button4_Click( object sender, EventArgs e)
{
通讯录 f = new 通讯录();
f.Show();
}
//3
private void textBox1_TextChanged( object sender, EventArgs e)
{
if (textBox1.Text.Length > 4)
button1.Enabled = false;
}
//3
private void button1_EnabledChanged( object sender, EventArgs e)
{
if (!button1.Enabled)
label3.Text = "请双击文本框重新登录!";
}
//4
private void textBox1_DoubleClick( object sender, EventArgs e)
{
textBox1.Text = "";
label3.Visible = false;
button1.Enabled = true;
}
//5
private void textBox1_Leave( object sender, EventArgs e)
{
if (textBox1.Text.Length > 6)
{
MessageBox.Show( "用户名超过6位");
textBox1.Text = "";
textBox1.Focus();
}
//1
else if (textBox1.Text.Length == 0)
textBox1.BackColor = Color.Yellow;
}
private void 登录_Load( object sender, EventArgs e)
{
//13
toolTip1.SetToolTip(textBox3, "这里显示网址!");
//14
GraphicsPath path = new GraphicsPath();
path.AddEllipse( this.button1.ClientRectangle);
button1.Region = new Region(path);
//产生验证码
GetValid();
label6.Font = new Font ( "宋体",9,FontStyle.Italic);
label6.BackColor = Color.Transparent;
17.设置窗体工作区的大小
//this.ClientSize = this.BackgroundImage.Size;
CodeImage(GetcheckCode());
}
//2
private void textBox1_Enter( object sender, EventArgs e)
{
//textBox1.BackColor = textBox2.BackColor;
textBox1.BackColor = Color.FromKnownColor(KnownColor.Window);
}
//6
private void textBox1_KeyPress( object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
textBox2.Focus();
}
//7
private void 登录_FormClosing( object sender, FormClosingEventArgs e)
{
DialogResult jg = MessageBox.Show( "你确定要关闭吗?", "关闭登录窗体", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (jg == DialogResult.No)
e.Cancel = true;
}
private void button5_Click( object sender, EventArgs e)
{
webBrowser1.Navigate(textBox3.Text.Trim());
}
//13
private void textBox2_MouseEnter( object sender, EventArgs e)
{
toolTip2.Show( "请输入密码!", this);
}
//15产生4个验证码
string valid;
void GetValid()
{
Random rad = new Random();
for ( int i = 0; i < 4; i++)
valid += rad.Next(0,9).ToString ();
label6.Text = valid;
}
//16.验证是数字的方法
bool IsNum( string s)
{
//1.使用异常检测的方法
//try
//{
// Convert .ToInt32(s);
//}
//catch
//{
// return false ;
//}
//return true ;
2.使用正则表达式
//string regex = @"^\d+(\.\d)?$";
//if (!Regex.IsMatch(s, regex))
// return false;
//else
// return true;
//3.调用VB的information的方法,判断是否为数字。先添加Microsoft.VisualBasic引用
return Microsoft.VisualBasic.Information.IsNumeric(s);
}
//16
private void textBox4_Leave( object sender, EventArgs e)
{
if (!IsNum(textBox4.Text)||textBox4.Text .Trim ().Length!=4)
{
textBox4.Text = "";
MessageBox.Show( "请输入正确的数字格式!");
}
}
//18
private void 登录_KeyDown( object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F4 && e.Modifiers == Keys.Alt)
e.Handled = true;
}
//产生随机数
string GetcheckCode()
{
int number;
char code;
string checkcode = "";
Random rdm = new Random();
for ( int i = 0; i < 4; i++)
{
number = rdm.Next();
//number = rdm.Next(0,9);
if (number % 2 == 0)
code = ( char)('0' + ( char)(number % 10));
else
code = ( char)('A' + ( char)(number % 26));
checkcode +="" +code.ToString ();
}
return checkcode;
}
void CodeImage( string code)
{
if (code == null || code.Trim() == string.Empty)
return;
System.Drawing.Bitmap image = new System.Drawing.Bitmap(( int)Math.Ceiling (code.Length*10.0),22);
Graphics g = Graphics.FromImage(image);
try
{
Random rdm = new Random();
g.Clear(Color.White);
//画图片的背景噪音线
for ( int i = 0; i < 3; i++)
{
int x1 = rdm.Next(image.Width);
int x2 = rdm.Next(image.Width);
int y1 = rdm.Next(image.Height);
int y2 = rdm.Next(image.Height);
g.DrawLine( new Pen(Color.Black), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font( "Arial", 12, FontStyle.Bold);
g.DrawString(code, font, new SolidBrush(Color.Red), 2, 2);
//画前景噪音线
for ( int i = 0; i < 150; i++)
{
int x = rdm.Next(image.Width);
int y = rdm.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(rdm.Next()));
}
g.DrawRectangle( new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
pictureBox1.Width = image.Width;
pictureBox1.Height = image.Height;
pictureBox1.Image = image;
}
catch
{ }
}
}
}
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.Collections;
using System.Drawing.Drawing2D;
using System.Text.RegularExpressions;
namespace 窗体练习
{
/*********************************
* 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.运行后,无法拖放窗体,两种方法
*/
public partial class 登录 : Form
{
public 登录()
{
InitializeComponent();
this.AcceptButton = button1; //8
this.CancelButton = button3;
this.ShowInTaskbar = false; //9
this.MaximizeBox = false; //10
//this.WindowState = FormWindowState.Maximized;//11
this.StartPosition = FormStartPosition.CenterScreen; //12
//this.FormBorderStyle = FormBorderStyle.FixedSingle;//19-1
//this.AutoSize = false;//19-2-1
//this.AutoSizeMode = AutoSizeMode.GrowAndShrink;//19-2-2
}
//登录判断
private void button1_Click( object sender, EventArgs e)
{
if (textBox1.Text == "a")
{
if (textBox2.Text == "a"&&textBox4.Text==valid)
{
button4.Enabled = true;
}
else
{
MessageBox.Show( "密码错误");
textBox2.Text = string.Empty;
}
}
else
{
if (textBox2.Text == "a")
{
MessageBox.Show( "用户名错误");
textBox1.Text = string.Empty;
}
else
{
MessageBox.Show( "用户名和密码错误");
textBox1.Text = string.Empty;
textBox2.Text = string.Empty;
textBox1.Focus();
}
}
}
//清空也可以使用string.Empty
private void button2_Click( object sender, EventArgs e)
{
textBox2.Text = "";
textBox1.Text = "";
}
//退出程序
private void button3_Click( object sender, EventArgs e)
{
/*当本窗口是主线程窗口,以下三种方法均可以退出程序
*当本窗口不是主窗口时,如果要退出整个程序,应采用第3种方法,将一次性关闭程序上存在的所有线程。
*如果只是想关闭当前窗口而不退出程序,应采用第一种方法
*当本窗口不是主窗口时,采用第2种方法退出程序时,将会再执行关闭主窗体的步骤。
*/
//this.Close();
//Application.Exit();
Application.ExitThread();
}
//弹出新的窗体
private void button4_Click( object sender, EventArgs e)
{
通讯录 f = new 通讯录();
f.Show();
}
//3
private void textBox1_TextChanged( object sender, EventArgs e)
{
if (textBox1.Text.Length > 4)
button1.Enabled = false;
}
//3
private void button1_EnabledChanged( object sender, EventArgs e)
{
if (!button1.Enabled)
label3.Text = "请双击文本框重新登录!";
}
//4
private void textBox1_DoubleClick( object sender, EventArgs e)
{
textBox1.Text = "";
label3.Visible = false;
button1.Enabled = true;
}
//5
private void textBox1_Leave( object sender, EventArgs e)
{
if (textBox1.Text.Length > 6)
{
MessageBox.Show( "用户名超过6位");
textBox1.Text = "";
textBox1.Focus();
}
//1
else if (textBox1.Text.Length == 0)
textBox1.BackColor = Color.Yellow;
}
private void 登录_Load( object sender, EventArgs e)
{
//13
toolTip1.SetToolTip(textBox3, "这里显示网址!");
//14
GraphicsPath path = new GraphicsPath();
path.AddEllipse( this.button1.ClientRectangle);
button1.Region = new Region(path);
//产生验证码
GetValid();
label6.Font = new Font ( "宋体",9,FontStyle.Italic);
label6.BackColor = Color.Transparent;
17.设置窗体工作区的大小
//this.ClientSize = this.BackgroundImage.Size;
CodeImage(GetcheckCode());
}
//2
private void textBox1_Enter( object sender, EventArgs e)
{
//textBox1.BackColor = textBox2.BackColor;
textBox1.BackColor = Color.FromKnownColor(KnownColor.Window);
}
//6
private void textBox1_KeyPress( object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
textBox2.Focus();
}
//7
private void 登录_FormClosing( object sender, FormClosingEventArgs e)
{
DialogResult jg = MessageBox.Show( "你确定要关闭吗?", "关闭登录窗体", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (jg == DialogResult.No)
e.Cancel = true;
}
private void button5_Click( object sender, EventArgs e)
{
webBrowser1.Navigate(textBox3.Text.Trim());
}
//13
private void textBox2_MouseEnter( object sender, EventArgs e)
{
toolTip2.Show( "请输入密码!", this);
}
//15产生4个验证码
string valid;
void GetValid()
{
Random rad = new Random();
for ( int i = 0; i < 4; i++)
valid += rad.Next(0,9).ToString ();
label6.Text = valid;
}
//16.验证是数字的方法
bool IsNum( string s)
{
//1.使用异常检测的方法
//try
//{
// Convert .ToInt32(s);
//}
//catch
//{
// return false ;
//}
//return true ;
2.使用正则表达式
//string regex = @"^\d+(\.\d)?$";
//if (!Regex.IsMatch(s, regex))
// return false;
//else
// return true;
//3.调用VB的information的方法,判断是否为数字。先添加Microsoft.VisualBasic引用
return Microsoft.VisualBasic.Information.IsNumeric(s);
}
//16
private void textBox4_Leave( object sender, EventArgs e)
{
if (!IsNum(textBox4.Text)||textBox4.Text .Trim ().Length!=4)
{
textBox4.Text = "";
MessageBox.Show( "请输入正确的数字格式!");
}
}
//18
private void 登录_KeyDown( object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F4 && e.Modifiers == Keys.Alt)
e.Handled = true;
}
//产生随机数
string GetcheckCode()
{
int number;
char code;
string checkcode = "";
Random rdm = new Random();
for ( int i = 0; i < 4; i++)
{
number = rdm.Next();
//number = rdm.Next(0,9);
if (number % 2 == 0)
code = ( char)('0' + ( char)(number % 10));
else
code = ( char)('A' + ( char)(number % 26));
checkcode +="" +code.ToString ();
}
return checkcode;
}
void CodeImage( string code)
{
if (code == null || code.Trim() == string.Empty)
return;
System.Drawing.Bitmap image = new System.Drawing.Bitmap(( int)Math.Ceiling (code.Length*10.0),22);
Graphics g = Graphics.FromImage(image);
try
{
Random rdm = new Random();
g.Clear(Color.White);
//画图片的背景噪音线
for ( int i = 0; i < 3; i++)
{
int x1 = rdm.Next(image.Width);
int x2 = rdm.Next(image.Width);
int y1 = rdm.Next(image.Height);
int y2 = rdm.Next(image.Height);
g.DrawLine( new Pen(Color.Black), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font( "Arial", 12, FontStyle.Bold);
g.DrawString(code, font, new SolidBrush(Color.Red), 2, 2);
//画前景噪音线
for ( int i = 0; i < 150; i++)
{
int x = rdm.Next(image.Width);
int y = rdm.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(rdm.Next()));
}
g.DrawRectangle( new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
pictureBox1.Width = image.Width;
pictureBox1.Height = image.Height;
pictureBox1.Image = image;
}
catch
{ }
}
}
}
本文转自叶子文文博客51CTO博客,原文链接http://blog.51cto.com/leafwf/185809如需转载请自行联系原作者
叶子文文