----------------------
Windows Phone 7手机开发、
.Net培训、期待与您交流! ----------------------
namespace 身份证检验
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
namespace 年月选择
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
namespace 两个数字之间的累加和
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
} ----------------------
Windows Phone 7手机开发、
.Net培训、期待与您交流! ----------------------详细请查看:
http://edu.youkuaiyun.com/heima/
1)身份证验证,检验是否成年
namespace 身份证检验
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void txtId_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar < '0' || e.KeyChar > '9')
{
//输入数字意外的字符就阻止掉
e.Handled = true;
}
//如果第18输入的是x,则不阻止
if ((txtId.SelectionStart == 17) && (e.KeyChar == 'x' || e.KeyChar == 'X'))
{
e.Handled = false;
}
//不阻止backspace
if (e.KeyChar == 8)
{
e.Handled = false;
}
}
{
if (e.KeyChar < '0' || e.KeyChar > '9')
{
//输入数字意外的字符就阻止掉
e.Handled = true;
}
//如果第18输入的是x,则不阻止
if ((txtId.SelectionStart == 17) && (e.KeyChar == 'x' || e.KeyChar == 'X'))
{
e.Handled = false;
}
//不阻止backspace
if (e.KeyChar == 8)
{
e.Handled = false;
}
}
private void butChakan_Click(object sender, EventArgs e)
{
int age = 0;
string id = txtId.Text.Trim();
if (id.Length == 15)
{
int year = Convert.ToInt32(id.Substring(6, 2)) + 1900;
age = DateTime.Now.Year - year;
}
else if (id.Length == 18)
{
if (!this.CheckCardId(id))
{
MessageBox.Show("身份证号码输入错误,请检查");
return;
}
int year = Convert.ToInt32(id.Substring(6, 4));
age = DateTime.Now.Year - year;
}
else
{
MessageBox.Show("身份证号码长度输入有误!");
return;
}
if (age >= 18)
{
pictureBox1.Visible = true;
}
else
{
MessageBox.Show("你太小了回家看动画片吧!");
}
}
/// <summary>
/// 校验身份证号.如果正确则返回true,否则返回false
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
{
int age = 0;
string id = txtId.Text.Trim();
if (id.Length == 15)
{
int year = Convert.ToInt32(id.Substring(6, 2)) + 1900;
age = DateTime.Now.Year - year;
}
else if (id.Length == 18)
{
if (!this.CheckCardId(id))
{
MessageBox.Show("身份证号码输入错误,请检查");
return;
}
int year = Convert.ToInt32(id.Substring(6, 4));
age = DateTime.Now.Year - year;
}
else
{
MessageBox.Show("身份证号码长度输入有误!");
return;
}
if (age >= 18)
{
pictureBox1.Visible = true;
}
else
{
MessageBox.Show("你太小了回家看动画片吧!");
}
}
/// <summary>
/// 校验身份证号.如果正确则返回true,否则返回false
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
private bool CheckCardId(string id)
{
{
int[] wQuan = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 };
string checkwei = "10X98765432";
string number17 = id.Substring(0, 17);
string number18 = id.Substring(17);
int sum = 0;
for (int i = 0; i < 17; i++)
{
sum = sum + Convert.ToInt32(number17[i].ToString()) * wQuan[i];
}
string checkwei = "10X98765432";
string number17 = id.Substring(0, 17);
string number18 = id.Substring(17);
int sum = 0;
for (int i = 0; i < 17; i++)
{
sum = sum + Convert.ToInt32(number17[i].ToString()) * wQuan[i];
}
int mod = sum % 11;
string result = checkwei[mod].ToString();
if (number18.Equals(result, StringComparison.OrdinalIgnoreCase))
{
return true;
}
else
{
return false;
}
string result = checkwei[mod].ToString();
if (number18.Equals(result, StringComparison.OrdinalIgnoreCase))
{
return true;
}
else
{
return false;
}
}
private void txtId_TextChanged(object sender, EventArgs e)
{
pictureBox1.Visible = false;
}
{
pictureBox1.Visible = false;
}
}
}
}
2)年月选择
namespace 年月选择
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//设置只能从列表中选择有效值,不让用户填写
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
{
//设置只能从列表中选择有效值,不让用户填写
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
for (int i = 1; i <= 12; i++)
{
comboBox1.Items.Add(i.ToString());
}
//初始化为0项
comboBox1.SelectedIndex = 0;
}
{
comboBox1.Items.Add(i.ToString());
}
//初始化为0项
comboBox1.SelectedIndex = 0;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.DropDownStyle = ComboBoxStyle.DropDownList;
int Maxday = 0;
switch (Convert.ToInt32(comboBox1.Text))
{
case 1: case 3: case 5: case 7: case 8: case 12:
Maxday = 31;
break;
case 2:
Maxday = 28;
break;
default :
Maxday = 30;
break;
}
comboBox2.Items.Clear();//清屏
{
comboBox2.DropDownStyle = ComboBoxStyle.DropDownList;
int Maxday = 0;
switch (Convert.ToInt32(comboBox1.Text))
{
case 1: case 3: case 5: case 7: case 8: case 12:
Maxday = 31;
break;
case 2:
Maxday = 28;
break;
default :
Maxday = 30;
break;
}
comboBox2.Items.Clear();//清屏
for (int i = 1; i <= Maxday; i++)
{
comboBox2.Items.Add(i.ToString());
}
comboBox2.SelectedIndex = 0;
{
comboBox2.Items.Add(i.ToString());
}
comboBox2.SelectedIndex = 0;
}
private void button1_Click(object sender, EventArgs e)
{
string str = string.Format("你选中了{0}月{1}日",comboBox1.Text,comboBox2.Text);
MessageBox.Show(str);
}
{
string str = string.Format("你选中了{0}月{1}日",comboBox1.Text,comboBox2.Text);
MessageBox.Show(str);
}
}
}
}
3)美女时钟
namespace 美女时钟
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
//y表示年 M月 d日 HH 24小时进制小时 m分钟 s秒
//当前目录下的MM目录
string filePath= this.Text =Application.StartupPath+" \\mm\\"+ DateTime.Now.ToString("HH_mm") + ".jpg";
this.pictureBox1.Image = Image.FromFile(filePath);
}
{
//y表示年 M月 d日 HH 24小时进制小时 m分钟 s秒
//当前目录下的MM目录
string filePath= this.Text =Application.StartupPath+" \\mm\\"+ DateTime.Now.ToString("HH_mm") + ".jpg";
this.pictureBox1.Image = Image.FromFile(filePath);
}
}
}
4)两个数字之间的累加和
namespace 两个数字之间的累加和
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int number1, number2;
if (!int.TryParse(Txtnumber1.Text, out number1))
{
//判断起始值是否为数字
MessageBox.Show("你输入的起始值错误,请重新输入");
Txtnumber1.Focus();
Txtnumber1.SelectAll();
return;
}
if (!int.TryParse(Txtnumber2.Text, out number2))
{
//判断终止值是否为数字
MessageBox.Show("你输入的终止值错误,请重新输入");
Txtnumber2.Focus();
Txtnumber2.SelectAll();
return;
}
if (number1 >= number2)
{
MessageBox.Show("起始值必须小于终止值");
return;
}
{
int number1, number2;
if (!int.TryParse(Txtnumber1.Text, out number1))
{
//判断起始值是否为数字
MessageBox.Show("你输入的起始值错误,请重新输入");
Txtnumber1.Focus();
Txtnumber1.SelectAll();
return;
}
if (!int.TryParse(Txtnumber2.Text, out number2))
{
//判断终止值是否为数字
MessageBox.Show("你输入的终止值错误,请重新输入");
Txtnumber2.Focus();
Txtnumber2.SelectAll();
return;
}
if (number1 >= number2)
{
MessageBox.Show("起始值必须小于终止值");
return;
}
int sum = 0;
for (int i = number1; i <= number2; i++)
{
//计算两个数字的累加和
sum = sum + i;
}
Txthe.Text = sum.ToString();
}
for (int i = number1; i <= number2; i++)
{
//计算两个数字的累加和
sum = sum + i;
}
Txthe.Text = sum.ToString();
}
private void Txtnumber1_TextChanged(object sender, EventArgs e)
{
Txthe.Text = "";
}
{
Txthe.Text = "";
}
private void button2_Click(object sender, EventArgs e)
{
//关闭文本框
this.Close();
}
{
//关闭文本框
this.Close();
}
private void Txtnumber1_KeyPress(object sender, KeyPressEventArgs e)
{
{
TextBox texbox = sender as TextBox;
//强转TextBox类型,子类转换成父类
//强转TextBox类型,子类转换成父类
if (e.KeyChar < '0' || e.KeyChar > '9')
{
//当输入的不是数字时阻止用户输入
e.Handled = true;
}
if (e.KeyChar == 8)
{
//如果等于8就是回车键
e.Handled = false;
}
if (texbox.SelectionStart == 0 && e.KeyChar == '0')
{
//当输入第一个数字为0时阻止输入
e.Handled = true;
}
}
{
//当输入的不是数字时阻止用户输入
e.Handled = true;
}
if (e.KeyChar == 8)
{
//如果等于8就是回车键
e.Handled = false;
}
if (texbox.SelectionStart == 0 && e.KeyChar == '0')
{
//当输入第一个数字为0时阻止输入
e.Handled = true;
}
}
private void Txthe_TextChanged(object sender, EventArgs e)
{
{
}
}
}