MaskedTextDateEx.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace LYFZ.ComponentLibrary
  9. {
  10. public partial class MaskedTextDateEx : MaskedTextBox
  11. {
  12. public MaskedTextDateEx()
  13. {
  14. InitializeComponent();
  15. }
  16. private string _strValue;
  17. /// <summary>
  18. /// 值
  19. /// </summary>
  20. public string StrValue
  21. {
  22. get
  23. {
  24. if (this.Text.Trim().Replace("-", "").Trim() != "")
  25. { _strValue = this.Text.Trim().Replace(" ", ""); }
  26. else
  27. { _strValue = this.Text.Trim().Replace("-", "").Trim(); }
  28. return _strValue;
  29. }
  30. set
  31. {
  32. _strValue = value;
  33. this.Text = _strValue;
  34. }
  35. }
  36. public MaskedTextDateEx(IContainer container)
  37. {
  38. container.Add(this);
  39. InitializeComponent();
  40. this.Mask = "0000-00-00";
  41. this.PromptChar = ' ';
  42. this.MouseClick += MaskedTextBoxEx_MouseClick;
  43. }
  44. /// <summary>
  45. /// 鼠标点击事件
  46. /// </summary>
  47. /// <param name="sender"></param>
  48. /// <param name="e"></param>
  49. private void MaskedTextBoxEx_MouseClick(object sender, MouseEventArgs e)
  50. {
  51. if (this.StrValue.Length < 10)
  52. { this.ForCharMaskedTextBox(); }
  53. }
  54. /// <summary>
  55. /// 重写输入限制
  56. /// </summary>
  57. /// <param name="e"></param>
  58. protected override void OnKeyPress(KeyPressEventArgs e)
  59. {
  60. if (LYFZ.BLL.OtherCommonModel.IsQaJiaoChar(e.KeyChar))
  61. { e.Handled = true; }
  62. else
  63. { base.OnKeyPress(e); }
  64. }
  65. /// <summary>
  66. /// 点击时在空值处获得光标
  67. /// </summary>
  68. private void ForCharMaskedTextBox()
  69. {
  70. char[] charList = this.Text.ToCharArray();
  71. bool isbl = true;
  72. for (int i = (charList.Length - 1); i >= 0; i--)
  73. {
  74. if (i == 0)
  75. {
  76. if (charList[i].ToString() != "-" && charList[i].ToString() != " ")
  77. {
  78. this.SelectionStart = i + 1;
  79. return;
  80. }
  81. }
  82. else
  83. {
  84. if (charList[i].ToString() != "-" && charList[i].ToString() != " ")
  85. {
  86. this.SelectionStart = i + 1;
  87. return;
  88. }
  89. }
  90. if (i == 0)
  91. {
  92. if (isbl)
  93. { this.SelectionStart = 0; }
  94. }
  95. }
  96. }
  97. public int CheckDateValue(MaskedTextDateEx maskDate, CheckBox chkEx = null, RadioButton rdoEx = null)
  98. {
  99. string StrchkExValue = "";
  100. if (chkEx != null)
  101. { StrchkExValue = chkEx.Checked.ToString().Trim(); }
  102. string StrrdoExValue = "";
  103. if (rdoEx != null)
  104. { StrrdoExValue = rdoEx.Checked.ToString().Trim(); }
  105. return this.CheckDateValue(maskDate.StrValue.Trim(), StrchkExValue, StrrdoExValue);
  106. }
  107. /// <summary>
  108. /// 检测日期数据
  109. /// 返回 0:无错误; 1:日期格式错误 2:若有农历框,当日期为02-29或02-30时,提示要勾上
  110. /// (如1提示:男宾生日日期格式输入错误
  111. /// 如2提示:男宾生日...不是公历日期,若要保存请勾上农历)
  112. ///
  113. /// </summary>
  114. /// <param name="maskDate">日期控件</param>
  115. /// <param name="chkEx">农历公历控件</param>
  116. /// <returns>返回 0:无错误; 1:日期格式错误 2:若有农历框,当日期为02-29或02-30时,提示要勾上</returns>
  117. public int CheckDateValue(string StrMaskDateValue, string StrchkExValue = "", string StrrdoExValue = "")
  118. {
  119. int returnValue = 0;
  120. if (!string.IsNullOrEmpty(StrMaskDateValue.Trim()))
  121. {
  122. bool IsDate = false;
  123. try
  124. {
  125. Convert.ToDateTime(StrMaskDateValue.Trim());
  126. string yyyy = StrMaskDateValue.Trim().Substring(0, 4);
  127. if (Convert.ToInt32(yyyy) < 1900)
  128. { IsDate = true; }
  129. }
  130. catch
  131. {
  132. if (StrMaskDateValue.Trim().Length == 10)
  133. {
  134. if (!string.IsNullOrEmpty(StrrdoExValue.Trim()) || !string.IsNullOrEmpty(StrchkExValue.Trim()))
  135. {
  136. string yyyy = StrMaskDateValue.Trim().Substring(0, 4);
  137. if (Convert.ToInt32(yyyy) >= 1900)
  138. {
  139. string MMdd = StrMaskDateValue.Trim().Substring(5, StrMaskDateValue.Trim().Length - 5);
  140. if (MMdd == "02-29" || MMdd == "02-30")
  141. {
  142. if (!string.IsNullOrEmpty(StrchkExValue.Trim()))
  143. {
  144. if (!Convert.ToBoolean(StrchkExValue))
  145. { returnValue = 2; }
  146. }
  147. else if (!string.IsNullOrEmpty(StrrdoExValue.Trim()))
  148. {
  149. if (!Convert.ToBoolean(StrrdoExValue))
  150. { returnValue = 2; }
  151. }
  152. }
  153. else { IsDate = true; }
  154. }
  155. else { IsDate = true; }
  156. }
  157. else { IsDate = true; }
  158. }
  159. else { IsDate = true; }
  160. }
  161. if (IsDate)
  162. { returnValue = 1; }
  163. }
  164. return returnValue;
  165. }
  166. /// <summary>
  167. /// 输入日期错误枚举
  168. /// </summary>
  169. public enum CheckDateValueErrorDescription
  170. {
  171. 无错误 = 0,
  172. 日期格式错误 = 1,
  173. 日期为02月29或02月30不是公历日期请勾上农历 = 2
  174. }
  175. }
  176. }