黑马程序员——学习WinForm写的几个小程序

本文介绍了一个C#应用程序,包括身份证验证、年龄计算、日期选择器、美女时钟及数字累加等功能。通过具体代码实现,展示了如何进行身份证的有效性验证、获取用户年龄,并通过条件判断显示不同的提示信息。
---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------
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;
            }
        }
        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>
        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];
            }
            int mod = sum % 11;
            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;
        }
    }
}
 
2)年月选择

namespace 年月选择
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            //设置只能从列表中选择有效值,不让用户填写
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            for (int i = 1; i <= 12; i++)
            {
                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();//清屏
            for (int i = 1; i <= Maxday; i++)
            {
                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);
        }
    }
}
 
3)美女时钟
namespace 美女时钟
{
    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);
        }
      
    }
}
 
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 sum = 0;
            for (int i = number1; i <= number2; i++)
            {
                //计算两个数字的累加和
                sum = sum + i;
            }
            Txthe.Text = sum.ToString();
        }
        private void Txtnumber1_TextChanged(object sender, EventArgs e)
        {
            Txthe.Text = "";
        }
        private void button2_Click(object sender, EventArgs e)
        {
            //关闭文本框
            this.Close();
        }
        private void Txtnumber1_KeyPress(object sender, KeyPressEventArgs e)
        {
            TextBox texbox = sender as 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;
            }
        }
        private void Txthe_TextChanged(object sender, EventArgs e)
        {
        }
 
    }
}
---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------详细请查看: http://edu.youkuaiyun.com/heima/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值