WCF醫院管理系統技術解析小功能(一)輸入身份證驗證年齡、性別、出生年月

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

此文章僅供學習,禁止用於商業用途,否則後果自負

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章