Fork me on GitHub

ErrorProvider

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;

namespace ErrorProvider
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private int a, b, c;
        private void txtName_TextChanged(object sender, EventArgs e)
        {
            if(txtName.Text=="")
            {
                errorProvider1.SetError(txtName,"姓名不能为空");
            }
            else if (txtName.Text.Trim().Length > 6)
            {
                MessageBox.Show("姓名太长了");
            }
            else
            {
                errorProvider1.SetError(txtName,"");
                a = 1;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void txtNumber_TextChanged(object sender, EventArgs e)
        {
            if (txtNumber.Text.Length > 10)
            {
                errorProvider1.SetError(txtNumber, "学号大于10位");
            }
            else
            {
                b = 1;
            }
        }

        private void txtAge_TextChanged(object sender, EventArgs e)
        {
            int age=Convert.ToInt32(txtAge.Text.Trim());
            if (age < 0 || age > 150)
            {
                errorProvider1.SetError(txtAge, "请确认年龄的合法性");
            }
            else
            {
                c = 1;
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtAge.Text = "";
            txtName.Text = "";
            txtNumber.Text = "";
        }

        private void btnSure_Click(object sender, EventArgs e)
        {
            if (a + b + c == 3)
            {
                MessageBox.Show("OK");
            }
        }
    }
}

posted @ 2013-05-14 21:19  种花生的读书人  阅读(272)  评论(0编辑  收藏  举报

该博客仅作为记录笔记,转载随意