下边是实现的代码:

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.Text.RegularExpressions;

namespace Registerting
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            txtUserName.Enter += new EventHandler(txtUserName_Enter);
            txtUserName.Leave += new EventHandler(txtUserName_Leave);
            txtPassword.Enter += new EventHandler(txtUserName_Enter);
            txtPassword.Leave += new EventHandler(txtPassWord_Leave);
            txtPWConfirm.Enter += new EventHandler(txtPWConfirm_Enter);
            txtPWConfirm.Leave += new EventHandler(txtPWConfirm_Leave);
            txtMail.Enter += new EventHandler(txtMail_Enter);
            txtMail.Leave += new EventHandler(txtMail_Leave);
            chkAll.Enter += new EventHandler(chkAll_Enter);
            chkAll.Leave += new EventHandler(chkAll_Leave);
        }
        /// <summary>
        /// txtUserName(用户名)失去焦点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtUserName_Leave(object sender, EventArgs e)
        {
            if (txtUserName.Text.Length < 1)
            {
                lblUNAttention.Visible = true;
                lblUNAttention.Text = "用户名不能为空";
            }
        }
        /// <summary>
        /// txtUserName(用户名)获得焦点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtUserName_Enter(object sender, EventArgs e)
        {
            if (txtUserName.Text.Length > 0)
            {
                lblUNAttention.Visible = false;
            }
        }
        /// <summary>
        /// txtPassWord(密码)失去焦点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtPassWord_Leave(object sender, EventArgs e)
        {
            if ((0 < txtPassword.Text.Length) && (txtPassword.Text.Length < 6))
            {
                lblPasswordAttenton.Visible = true;
                lblPasswordAttenton.Text = "密码不能少于六位";
            }
            else if (txtPassword.Text.Length < 1)
            {
                lblPasswordAttenton.Visible = true;
                lblPasswordAttenton.Text = "密码不能为空";
            }
        }
        /// <summary>
        /// txtPassWord(密码)获得焦点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtPassWord_Enter(object sender, EventArgs e)
        {
            if (txtUserName.Text.Length > 0)
            {
                lblUNAttention.Visible = false;
            }
            else
            {
                lblUNAttention.Visible = true;
            }
            lblPasswordAttenton.Visible = false;
        }
        /// <summary>
        /// txtPWConfirm(密码)失去焦点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtPWConfirm_Enter(object sender, EventArgs e)
        {
            if (txtUserName.Text.Length > 0)
            {
                lblUNAttention.Visible = false;
            }
            else
            {
                lblUNAttention.Visible = true;
            }

if (txtPassword.Text.Length > 0)
            {
                lblPasswordAttenton.Visible = false;
            }
            else
            {
                lblPasswordAttenton.Visible = true;
            }
        }
        /// <summary>
        /// txtPWConfirm(密码)获得焦点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtPWConfirm_Leave(object sender, EventArgs e)
        {
            if (txtPWConfirm.Text.Length < 1)
            {
                lblPWAttionC.Text = "确认密码输入不能为空";
                lblPWAttionC.Visible = true;
            }
            else if (txtPWConfirm.Text != txtPassword.Text)
            {
                lblPWAttionC.Text = "两次密码输入不一致,\n请重新输入";
                lblPWAttionC.Visible = true;
            }
            else if (txtPWConfirm.Text == txtPassword.Text)
            {
                lblPWAttionC.Visible = false;
            }
        }

private void txtMail_Enter(object sender, EventArgs e)
        {
            lblMAttion.Visible = false;
        }

private void txtMail_Leave(object sender, EventArgs e)
        {
            string reg = @"^\s*([A-Za-z0-9_-]+(\.\w+)*@([\w-]+\.)+\w{2,10})\s*$";
            if (txtMail.Text.Length < 1)
            {
                lblMAttion.Text = "邮箱输入不能为空";
                lblMAttion.Visible = true;
            }
            else if (Regex.IsMatch(txtMail.Text, reg) == false)
            {
                lblMAttion.Text = "邮箱输入不合法";
                lblMAttion.Visible = true;
            }
        }

private void chkAll_Enter(object sender, EventArgs e)
        {
            lblHobbyAttention.Visible = false;
        }

private void chkAll_Leave(object sender, EventArgs e)
        {
            int sum = 0;
            for (int i = 0; i < chklEate.Items.Count; i++)
            {
                if (chklEate.GetItemChecked(i) == true)
                {
                    sum = sum + 1;
                }
            }
            if (sum < 1)
            {
                lblHobbyAttention.Visible = true;
                lblHobbyAttention.Text = "您还没有选择任何爱好";
            }
        }
        /// <summary>
        /// 全选或者全不选按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkAll_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < chklEate.Items.Count; i++)
            {
                if (chkAll.Checked == true)
                {
                    //全选
                    chklEate.SetItemChecked(i, true);
                }
                else
                {
                    //全不选
                    chklEate.SetItemChecked(i, false);
                }
            }
        }
        /// <summary>
        /// 提交注册信息,如果注册信息都满足条件,那么提交成功,如果没有,则注册失败
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRegister_Click(object sender, EventArgs e)
        {
            //邮箱的正则表达式
            string reg = @"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$";
            int sum = 0;
            //计算选择的爱好数目
            for (int i = 0; i < chklEate.Items.Count; i++)
            {
                if (chklEate.GetItemChecked(i) == true)
                {
                    sum = sum + 1;
                }
            }
            //判断注册的所有信息是否合法,合法就注册成功,并关闭窗口
            if ((txtUserName.Text.Length > 0) && (txtPassword.Text.Length > 5) && (txtPassword.Text == txtPWConfirm.Text) && (Regex.IsMatch(txtMail.Text, reg) == true) && (sum > 0))
            {
               DialogResult dr = MessageBox.Show("您注册成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
               if (dr == DialogResult.OK)
               {
                   this.Close();
               }
            }
            else
            {
                MessageBox.Show("您注册失败!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /// <summary>
        /// 清空输入信息和提示信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConcel_Click(object sender, EventArgs e)
        {
            txtUserName.Clear();
            lblUNAttention.Text = "";
            txtPassword.Clear();
            lblPasswordAttenton.Text = "";
            txtPWConfirm.Clear();
            lblPWAttionC.Text = "";
            txtMail.Clear();
            lblMAttion.Text = "";
            chklEate.ClearSelected();
            lblHobbyAttention.Text = "";
            chkAll.Checked = false;
        }
    }
}

今天用C#做的一个小的注册练习的更多相关文章

  1. FMX相当于在界面上自己又做了一个小操作系统

    FMX的自画界面我也不看好,比如复制粘贴,太丑了,系统做得很好很精细的复制粘贴界面,就是无法调出,比如MIUI,复制粘贴还能有个放大镜,可以选择到屏幕边缘的文字,可以选择剪贴板内多个可粘贴的文字:还有 ...

  2. DOM的利用冒泡做的一个小程序

    我们都知道DOM的事件流,有冒泡事件,如何有效的利用冒泡? 优化:应该尽量少的添加事件监听:原理:每添加一个事件监听事件,就会在浏览器中添加一个EventListener,如果数量过多,浏览器只能一个 ...

  3. 加班两个星期做的一个小系统~(winform)

    不管怎么样~加班两个星期,单独一人,努力将公司需要用的系统给做出来了,也感谢提供技术帮助的可爱人儿~ 首先,系统有个检测版本的功能,若版本不是最新的,则会自动更新(公司要求,必须强制更新)~ 更新界面 ...

  4. 基于Tkinter以及百度翻译爬虫做的一个小的翻译软件

    首先看效果: 输入Hello, 可见输出 int. 打招呼 下面看源码: from tkinter import * import requests# 首先导入用到的库 request = reque ...

  5. 对于xss等有关的html,url,unicode编码做的一个小总结。

    参考:http://bobao.360.cn/learning/detail/292.html,算是对前部分作一个总结性的学习. 1<a href="%6a%61%76%61%73%6 ...

  6. Angularjs做的一个小页面

    <!DOCTYPE html><html lang="en" ng-app="todolist"> <head> <m ...

  7. 自己做的一个小demo

    上图: 主段代码: <script type="text/javascript"> var getRandomColor = function(){ return (f ...

  8. 用Unity做的一个小游戏,仿照一个样例写的,个人认为文章写的不错,哈哈

  9. jquery做一个小的轮播插件---有BUG,后续修改

    //首页无缝轮播 ; (function($, window, document, undefined) { $.fn.slider = function(options) { var default ...

随机推荐

  1. oracle_深刻理解数据库的启动和关闭

    Oracle数据库提供了几种不同的数据库启动和关闭方式,本文将详细介绍这些启动和关闭方式之间的区别以及它们各自不同的功能. 一.启动和关闭Oracle数据库 对于大多数Oracle DBA来说,启动和 ...

  2. linux_曝出重大bash安全漏洞及修复方法

    日前Linux官方内置Bash中新发现一个非常严重安全漏洞(漏洞参考https://access.redhat.com/security/cve/CVE-2014-6271  ),黑客可以利用该Bas ...

  3. 从头开始学JavaScript (二)——变量及其作用域

    原文:从头开始学JavaScript (二)--变量及其作用域 一.变量 ECMAscript变量是松散型变量,所谓松散型变量,就是变量名称可以保存任何类型的数据,每个变量仅仅是一个用于保存值的占位符 ...

  4. Sicily 1732 Alice and Bob (二进制最大公约数)

    联系: http://soj.me/1732 Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description: Alice is a b ...

  5. 我的MYSQL学习心得(十)

    原文:我的MYSQL学习心得(十) 我的MYSQL学习心得(十) 我的MYSQL学习心得(一) 我的MYSQL学习心得(二) 我的MYSQL学习心得(三) 我的MYSQL学习心得(四) 我的MYSQL ...

  6. C#二维码生成解析

    C#二维码生成解析工具,可添加自定义Logo 二维码又称 QR Code,QR 全称 Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的 Bar Code 条形码能 ...

  7. JS时间戳比较大小:对于一组时间戳(开始时间~结束时间)和另一组时间戳进行比较,用于判断被比较时间戳组是否在要求范围内

    /* *JS时间戳比较大小:对于一组时间戳(开始时间~结束时间)和另一组时间戳进行比较,用于判断被比较时间戳组是否在要求范围内 *@param date1 date2(形如:'2015-01-01'类 ...

  8. HTML5 五彩圆环Loading加载动画实现教程

    原文:HTML5 五彩圆环Loading加载动画实现教程 今天我们要来介绍一款效果很特别的HTML5 Loading加载动画,不像其他的Loading动画,这款Loading动画颜色很丰富,并且在转圈 ...

  9. ubuntu eclipse android搭建

    1.eclipse加入android adt: 终端:sudo gedit /etc/hosts 加入: #for android 173.194.72.93 dl.google.com 173.19 ...

  10. Asp.net 4.0,首次请求目录下的文件时响应很慢

    原文:Asp.net 4.0,首次请求目录下的文件时响应很慢 1. 问题起因2. 尝试过的处理思路3. 解决方法 1. 问题起因 一个从VS2003(.Net Framework 1.1)升级到.ne ...