您的位置:首页 > 其它

WCF医院管理系统技术解析小功能(一)输入身份证验证年龄、性别、出生年月

2015-06-01 21:44 405 查看
WCF医院管理系统技术解析小功能(一)输入身份证验证年龄、性别、出生年月

体检登记中的新增病人界面输入中,输入身份证号

获取性别、年龄、出生年月

:如图:



2.9.8(图50)

用到的主要控件有:

控件

说明

文本框 (TextBox)

从工具箱中找到对应的控件,可以设置控件的一些属性和事件。如显示的文本,命名是规范

下拉框 (ComBobox)

该过程不涉及数据库和BLL,但是要在UIL中需要添加一个CSV文件:



第一步:点击WCF医院管理系统_Client右键



第二步选择Windows资源管理器中打开文件夹



2.9.8(图51)

第三步点击bin后,点击Debug



2.9.8(图52)

第四步把用到的inif的后缀为CSV文件复制好粘贴进bin中的Debug文件中



2.9.8(图53)

然后在using中添加一个引用:



2.9.8(图54)

UIL中的代码有:

全局变量中添加一个数组:

IDS[] id = new IDS[4400];

在窗体加载事件中的代码:

StreamReader sr = new StreamReader("inif.csv", Encoding.Default);//用特定的编码从字节流中读取字符 其中"inif.csv"是刚才添加的文件
string str = "";
string a, b, c, d, e2;
string[] str_temp;//声明一个数组
int index = 0;
while (!sr.EndOfStream)
{
str = sr.ReadLine().Trim();
str_temp = str.Split(',');
a = str_temp[0].Trim();
b = str_temp[1].Trim();
c = str_temp[2].Trim();
d = str_temp[3].Trim();
e2 = str_temp[4].Trim();
id[index++] = new IDS(a, b, c, d, e2);
}
sr.Close();


自定义方法中写:

public struct IDS
{
public string dmmc;
public string dmzm;
public string dmbz;
public string dmxh;
public string dmmcl;
public IDS(string a, string b, string c, string d, string e)
{
this.dmmc = a;
this.dmzm = b;
this.dmbz = c;
this.dmxh = d;
this.dmmcl = e;
}
}


在身份证中输入的文本框

中找到TextChanged事件并在其中写如下代码:

private void txft_IDCardNo_TextChanged(object sender, EventArgs e)
{
string keys = txt_IDCardNo.Text;
#region 判断身份证中的年,月,日
#region 判断年份
if (txt_IDCardNo.TextLength == 10)
{
string birth_y = keys.Substring(6, 4);//获取身份证中输入的年份
decimal decbirth_y = Convert.ToDecimal(birth_y);//将获取的年份传换为小数
string strNow = DateTime.Now.Year.ToString();//获取现在的年份
decimal decNow = Convert.ToDecimal(strNow);//将获取的年份转换为小数
if (decbirth_y > (decNow - 18) || decbirth_y < (decNow - 200))//判断身份证中的有效年份
{
MessageBox.Show("身份证中的年份有误!请重新输入");
txt_IDCardNo.Text = "";
return;
}
}
#endregion
#region 判断月份
if (txt_IDCardNo.TextLength == 12)
{
string strYue = keys.Substring(10, 2);//获取身份证中的月份
{
int intYueFenFalse = 0;
string[] strShuZu = new string[] { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" };//声明一个月份的数组
for (int i = 0; i < strShuZu.Length; i++)//循环判断身份证中的月份为数组中的具体月份
{
if (strYue != strShuZu[i])//如果身份证中的月份没有雨数组中的对应
{
intYueFenFalse++;//累加变量
}
}
if (intYueFenFalse == strShuZu.Length)//累加的变量如果等于月份数组的长度,则为无效身份证
{
MessageBox.Show("你输入的月份与现实不符,请重新输入!");
txt_IDCardNo.Text = "";
return;
}
}
}
#endregion
#region 判断日期
if (txt_IDCardNo.TextLength == 14)
{
int intShuZuChangDu = 0;
string birth_d = keys.Substring(12, 2);//截取身份证输入的日期号
int intRiHao = 0;
string[] StrShuZu = new string[] {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12",
"13", "14", "15", "16", "17", "18", "19", "20", "21", "22" ,"23", "24", "25", "26", "27", "28", "29", "30", "31" };//声明一个日期号的数组
string strXiaoYue = "";//声明一个变量获取小月份
string strYue = keys.Substring(10, 2);//获取身份证中的月份
string[] strYueShuZu = new string[] { "02", "04", "06", "09", "11" };//声明一个小月的月份数组
for (int y = 0; y < strYueShuZu.Length; y++)///循环判断身份证中的月份为数组中的具体月份
{
if (strYue == strYueShuZu[y])//如果身份证中的月份与等于小月中的某一月份
{
strXiaoYue = strYue;//将身份证中的月份赋值给小月份变量
break;//跳出循环
}
}
if (strXiaoYue == "")//如果小月份为空,
{
intShuZuChangDu = StrShuZu.Length;//日号的数组长度为正常长度
}
else
{
if (strXiaoYue == "02")//如果月份为2月,
{
string birth_y = keys.Substring(6, 4);//截取身份证中的年份
decimal decbirth_y = Convert.ToDecimal(birth_y);//将年份转换为小数的类型
if (decbirth_y / 4 == 0)//如果年份/4为0,就为闰年,此时2月有29天
{
intShuZuChangDu = StrShuZu.Length - 2;//日号数组的长度-2
}
else
{
intShuZuChangDu = StrShuZu.Length - 3;//否则为平年,2月有28天日号数组-3
}
}
else //小月份不等于2月就是等于其他小月
{
intShuZuChangDu = StrShuZu.Length - 1;//此时日号长度-1;
}
}
if (intShuZuChangDu > 0)//对日号进行判断
{
for (int i = 0; i < intShuZuChangDu; i++)//循环判断身份证中的日号为数组中的具体日号
{
if (birth_d != StrShuZu[i])//如果身份证中的日期没有与数组中的对应
{
intRiHao++;//累加变量
}
}
if (intRiHao == intShuZuChangDu)//累加的变量如果等于日期数组的长度,则为无效身份证日号
{
MessageBox.Show("你输入的日期号与现实不符,请重新输入!");
txt_IDCardNo.Text = "";
return;
}
}
}
#endregion
#endregion

if (txt_IDCardNo.TextLength == 18)
{

bool all_pass = true;
if (all_pass != true)
{
MessageBox.Show("你输入的身份证号码有误,已用红色标注,请修改!!");
return;
}

string birth_m = keys.Substring(10, 2);//截取身份证中的10位到12位数字  即是月份
string birth_d = keys.Substring(12, 2);//截取身份证中的12位到14位数字   即是日期号
string dmzm = keys.Substring(0, 6);//截取身份证中的前6位数字
int sex = int.Parse(keys.Substring(16, 1));//截取身份证中的第17位数字   用于判断性别
string birth_y = keys.Substring(6, 4);//截取身份证中的6位到10位数字     即是年份
decimal decbirth_y = Convert.ToDecimal(birth_y);// 转化截取的年份
string strNow = DateTime.Now.Year.ToString();//获取当前年份
decimal decNow = Convert.ToDecimal(strNow);//转化当前年份

ListViewItem l = new ListViewItem();
foreach (IDS i in id)//遍历循环身份证中的输入的长度
{
if (i.dmzm.Equals(dmzm))
{
l.SubItems.Add(i.dmmcl);
txt_Birthday.Text = birth_y + "年" + birth_m + "月" + birth_d + "日";//并接出生日期
decimal decAge = Convert.ToDecimal(decNow - decbirth_y);
txt_Age.Text = decAge.ToString().Trim();//
if (sex % 2 == 0)//判断性别
{
l.SubItems.Add("女");
cbo_AS_SexID.SelectedValue = 5;
}
else
{
l.SubItems.Add("男");
cbo_AS_SexID.SelectedValue = 4;

}
break;
}
}
}
}


此文章仅供学习,禁止用于商业用途,否则后果自负
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: