录入学员的身份证后控件焦点转移时根据身份证号码获得生日和性别

/// <summary>
///在控件验证 textBox_IdentityCard 的 Validated事件中定义身份证号码的合法性并根据身份证号码得到生日和性别

/// </summary>       

       
private void textBox_IdentityCard_Validated(object sender, EventArgs e)

       {
           
try
           {
               
string identityCard = textBox_IdentityCard.Text.Trim();//获取得到输入的身份证号码

               
if (string.IsNullOrEmpty(identityCard))
               {
                   MessageBox.Show(
"身份证号码不能为空!");//身份证号码不能为空,如果为空返回
                   if (textBox_IdentityCard.CanFocus)
                   {

                       textBox_IdentityCard.Focus();
//设置当前输入焦点为textBox_IdentityCar

                   }
                   
return;
                   }
               
else
               {
                   
if (identityCard.Length != 15 && identityCard.Length != 18)//身份证号码只能为15位或18位其它不合法
                   {

                       MessageBox.Show(
"身份证号码为15位或18位,请检查!");

                       
if (textBox_IdentityCard.CanFocus)
                       {
                           textBox_IdentityCard.Focus();
                       }
                       
return;
                    }
               }

               
string birthday = "";
               
string sex = "";
               
if (identityCard.Length == 18)//处理18位的身份证号码从号码中得到生日和性别代码
              {
                   birthday 
= identityCard.Substring(64+ "-" + identityCard.Substring(102+ "-" + identityCard.Substring(122);
                   sex 
= identityCard.Substring(143);
               }
               
if (identityCard.Length == 15)
               {

                   birthday 
= "19" + identityCard.Substring(62+ "-" + identityCard.Substring(82+ "-" + identityCard.Substring(102);
                   sex 
= identityCard.Substring(123);
               }
               textBox_Birthday.Text 
= birthday;
               
if (int.Parse(sex) % 2 == 0)//性别代码为偶数是女性奇数为男性
               {
                   
this.comboBox_Sex.Text = "";

              }
               
else
               {
                   
this.comboBox_Sex.Text = "";
               }
           }
           
catch (Exception ex)
           {
               MessageBox.Show(
"身份证号码输入有误");
               
if (textBox_IdentityCard.CanFocus)
               {
                   textBox_IdentityCard.Focus();
               }
               
return;
           }
       }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值