今天用C#做的一个小的注册练习

下边是实现的代码:

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;
        }
    }
}

 

posted @ 2013-07-16 11:31  秋日的私语(且行且珍惜)  阅读(155)  评论(0编辑  收藏  举报