TxtCodeNo.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. 
  2. using System.Drawing;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using System.Windows.Forms;
  6. namespace Dongke.WinForm.Controls
  7. {
  8. /// <summary>
  9. /// 编号输入文本框(数字、字母和特殊字符)
  10. /// </summary>
  11. [ToolboxBitmap(typeof(TextBox))]
  12. public class TxtCodeNo : TextBoxCodeBase
  13. {
  14. #region 常量
  15. private static string CODE_NO_REGULAR = @"^(?:[\D]*|[\S\s]*[\D]+)(?<no>[\d]+)(?:[\D]*)$";
  16. #endregion
  17. #region 构造函数
  18. /// <summary>
  19. /// 编号输入文本框(数字、字母和特殊字符)
  20. /// </summary>
  21. public TxtCodeNo()
  22. {
  23. this.Regular = this.GetCodeRegular();
  24. }
  25. #endregion
  26. #region 保护方法
  27. /// <summary>
  28. /// 获取编号正则表达式
  29. /// </summary>
  30. /// <returns></returns>
  31. protected virtual string GetCodeRegular()
  32. {
  33. return CODE_NO_REGULAR;
  34. }
  35. #endregion
  36. #region 公有方法
  37. /// <summary>
  38. /// 获取下一个编号
  39. /// </summary>
  40. /// <param name="value">增加量</param>
  41. /// <param name="clip">数值超长后是否截断</param>
  42. /// <returns>新编号</returns>
  43. public string GetNextCodeNo(int value = 1, bool clip = false)
  44. {
  45. string codeFormat = null;
  46. decimal number = 0;
  47. int length = 0;
  48. if (this.GetMatchCodeNo(out codeFormat, out number, out length))
  49. {
  50. return this.GetNextCodeNo(codeFormat, number + value, length, clip);
  51. }
  52. return null;
  53. }
  54. #endregion
  55. #region 私有方法
  56. /// <summary>
  57. /// 获取条码格式
  58. /// </summary>
  59. /// <param name="codeFormat">编号格式化</param>
  60. /// <param name="number">数值部分</param>
  61. /// <param name="length">数值部分长度</param>
  62. /// <returns>条码格式是否正确</returns>
  63. private bool GetMatchCodeNo(out string codeFormat, out decimal number, out int length)
  64. {
  65. return TxtCodeNo.GetMatchCodeNo(this.Text, this.Regular, out codeFormat, out number, out length);
  66. }
  67. /// <summary>
  68. /// 获取下一个编号
  69. /// </summary>
  70. /// <param name="codeFormat">编号格式化</param>
  71. /// <param name="number">数值部分</param>
  72. /// <param name="length">数值部分长度</param>
  73. /// <param name="clip">数值超长后是否截断</param>
  74. /// <returns>新编号</returns>
  75. private string GetNextCodeNo(string codeFormat, decimal number, int length, bool clip = false)
  76. {
  77. string no = number.ToString(string.Empty.PadLeft(length, Constant.C_0));
  78. if (clip && no.Length > length)
  79. {
  80. no = no.Substring(no.Length - length, length);
  81. }
  82. return string.Format(codeFormat, no);
  83. }
  84. #endregion
  85. #region 静态方法
  86. /// <summary>
  87. /// 获取编号格式
  88. /// </summary>
  89. /// <param name="codeNo">编号</param>
  90. /// <param name="codeRegular">编号正则表达式</param>
  91. /// <param name="codeFormat">编号格式化</param>
  92. /// <param name="number">数值部分</param>
  93. /// <param name="length">数值部分长度</param>
  94. /// <returns>编号格式是否正确</returns>
  95. private static bool GetMatchCodeNo(string codeNo, string codeRegular,
  96. out string codeFormat, out decimal number, out int length)
  97. {
  98. codeFormat = codeNo;
  99. number = 0;
  100. length = 0;
  101. bool result = false;
  102. if (string.IsNullOrWhiteSpace(codeNo) ||
  103. string.IsNullOrWhiteSpace(codeRegular) ||
  104. !codeRegular.Contains("?<no>"))
  105. {
  106. return result;
  107. }
  108. Match match = Regex.Match(codeNo, codeRegular);
  109. if (match.Success)
  110. {
  111. StringBuilder sb = new StringBuilder();
  112. Group noGroup = match.Groups["no"];
  113. if (noGroup.Success)
  114. {
  115. string no = noGroup.Value;
  116. if (decimal.TryParse(no, out number))
  117. {
  118. result = true;
  119. int index = noGroup.Index;
  120. length = noGroup.Length;
  121. sb.Append(codeNo.Substring(0, index));
  122. sb.Append("{0}");
  123. sb.Append(codeNo.Substring(index + length));
  124. codeFormat = sb.ToString();
  125. }
  126. }
  127. }
  128. return result;
  129. }
  130. #endregion
  131. }
  132. }