123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace LYFZ.ComponentLibrary
- {
- public partial class MaskedTextDateEx : MaskedTextBox
- {
- public MaskedTextDateEx()
- {
- InitializeComponent();
- }
- private string _strValue;
- /// <summary>
- /// 值
- /// </summary>
- public string StrValue
- {
- get
- {
- if (this.Text.Trim().Replace("-", "").Trim() != "")
- { _strValue = this.Text.Trim().Replace(" ", ""); }
- else
- { _strValue = this.Text.Trim().Replace("-", "").Trim(); }
- return _strValue;
- }
- set
- {
- _strValue = value;
- this.Text = _strValue;
- }
- }
- public MaskedTextDateEx(IContainer container)
- {
- container.Add(this);
- InitializeComponent();
- this.Mask = "0000-00-00";
- this.PromptChar = ' ';
- this.MouseClick += MaskedTextBoxEx_MouseClick;
- }
- /// <summary>
- /// 鼠标点击事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void MaskedTextBoxEx_MouseClick(object sender, MouseEventArgs e)
- {
- if (this.StrValue.Length < 10)
- { this.ForCharMaskedTextBox(); }
- }
- /// <summary>
- /// 重写输入限制
- /// </summary>
- /// <param name="e"></param>
- protected override void OnKeyPress(KeyPressEventArgs e)
- {
- if (LYFZ.BLL.OtherCommonModel.IsQaJiaoChar(e.KeyChar))
- { e.Handled = true; }
- else
- { base.OnKeyPress(e); }
- }
- /// <summary>
- /// 点击时在空值处获得光标
- /// </summary>
- private void ForCharMaskedTextBox()
- {
- char[] charList = this.Text.ToCharArray();
- bool isbl = true;
- for (int i = (charList.Length - 1); i >= 0; i--)
- {
- if (i == 0)
- {
- if (charList[i].ToString() != "-" && charList[i].ToString() != " ")
- {
- this.SelectionStart = i + 1;
- return;
- }
- }
- else
- {
- if (charList[i].ToString() != "-" && charList[i].ToString() != " ")
- {
- this.SelectionStart = i + 1;
- return;
- }
- }
- if (i == 0)
- {
- if (isbl)
- { this.SelectionStart = 0; }
- }
- }
- }
- public int CheckDateValue(MaskedTextDateEx maskDate, CheckBox chkEx = null, RadioButton rdoEx = null)
- {
- string StrchkExValue = "";
- if (chkEx != null)
- { StrchkExValue = chkEx.Checked.ToString().Trim(); }
- string StrrdoExValue = "";
- if (rdoEx != null)
- { StrrdoExValue = rdoEx.Checked.ToString().Trim(); }
- return this.CheckDateValue(maskDate.StrValue.Trim(), StrchkExValue, StrrdoExValue);
- }
- /// <summary>
- /// 检测日期数据
- /// 返回 0:无错误; 1:日期格式错误 2:若有农历框,当日期为02-29或02-30时,提示要勾上
- /// (如1提示:男宾生日日期格式输入错误
- /// 如2提示:男宾生日...不是公历日期,若要保存请勾上农历)
- ///
- /// </summary>
- /// <param name="maskDate">日期控件</param>
- /// <param name="chkEx">农历公历控件</param>
- /// <returns>返回 0:无错误; 1:日期格式错误 2:若有农历框,当日期为02-29或02-30时,提示要勾上</returns>
- public int CheckDateValue(string StrMaskDateValue, string StrchkExValue = "", string StrrdoExValue = "")
- {
- int returnValue = 0;
- if (!string.IsNullOrEmpty(StrMaskDateValue.Trim()))
- {
- bool IsDate = false;
- try
- {
- Convert.ToDateTime(StrMaskDateValue.Trim());
- string yyyy = StrMaskDateValue.Trim().Substring(0, 4);
- if (Convert.ToInt32(yyyy) < 1900)
- { IsDate = true; }
- }
- catch
- {
- if (StrMaskDateValue.Trim().Length == 10)
- {
- if (!string.IsNullOrEmpty(StrrdoExValue.Trim()) || !string.IsNullOrEmpty(StrchkExValue.Trim()))
- {
- string yyyy = StrMaskDateValue.Trim().Substring(0, 4);
- if (Convert.ToInt32(yyyy) >= 1900)
- {
- string MMdd = StrMaskDateValue.Trim().Substring(5, StrMaskDateValue.Trim().Length - 5);
- if (MMdd == "02-29" || MMdd == "02-30")
- {
- if (!string.IsNullOrEmpty(StrchkExValue.Trim()))
- {
- if (!Convert.ToBoolean(StrchkExValue))
- { returnValue = 2; }
- }
- else if (!string.IsNullOrEmpty(StrrdoExValue.Trim()))
- {
- if (!Convert.ToBoolean(StrrdoExValue))
- { returnValue = 2; }
- }
- }
- else { IsDate = true; }
- }
- else { IsDate = true; }
- }
- else { IsDate = true; }
- }
- else { IsDate = true; }
- }
- if (IsDate)
- { returnValue = 1; }
- }
- return returnValue;
- }
- /// <summary>
- /// 输入日期错误枚举
- /// </summary>
- public enum CheckDateValueErrorDescription
- {
- 无错误 = 0,
- 日期格式错误 = 1,
- 日期为02月29或02月30不是公历日期请勾上农历 = 2
- }
- }
- }
|