| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
-
- using System.Drawing;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Windows.Forms;
- namespace Dongke.WinForm.Controls
- {
- /// <summary>
- /// 编号输入文本框(数字、字母和特殊字符)
- /// </summary>
- [ToolboxBitmap(typeof(TextBox))]
- public class TxtCodeNo : TextBoxCodeBase
- {
- #region 常量
- private static string CODE_NO_REGULAR = @"^(?:[\D]*|[\S\s]*[\D]+)(?<no>[\d]+)(?:[\D]*)$";
- #endregion
- #region 构造函数
- /// <summary>
- /// 编号输入文本框(数字、字母和特殊字符)
- /// </summary>
- public TxtCodeNo()
- {
- this.Regular = this.GetCodeRegular();
- }
- #endregion
- #region 保护方法
- /// <summary>
- /// 获取编号正则表达式
- /// </summary>
- /// <returns></returns>
- protected virtual string GetCodeRegular()
- {
- return CODE_NO_REGULAR;
- }
- #endregion
- #region 公有方法
- /// <summary>
- /// 获取下一个编号
- /// </summary>
- /// <param name="value">增加量</param>
- /// <param name="clip">数值超长后是否截断</param>
- /// <returns>新编号</returns>
- public string GetNextCodeNo(int value = 1, bool clip = false)
- {
- string codeFormat = null;
- decimal number = 0;
- int length = 0;
- if (this.GetMatchCodeNo(out codeFormat, out number, out length))
- {
- return this.GetNextCodeNo(codeFormat, number + value, length, clip);
- }
- return null;
- }
- #endregion
- #region 私有方法
- /// <summary>
- /// 获取条码格式
- /// </summary>
- /// <param name="codeFormat">编号格式化</param>
- /// <param name="number">数值部分</param>
- /// <param name="length">数值部分长度</param>
- /// <returns>条码格式是否正确</returns>
- private bool GetMatchCodeNo(out string codeFormat, out decimal number, out int length)
- {
- return TxtCodeNo.GetMatchCodeNo(this.Text, this.Regular, out codeFormat, out number, out length);
- }
- /// <summary>
- /// 获取下一个编号
- /// </summary>
- /// <param name="codeFormat">编号格式化</param>
- /// <param name="number">数值部分</param>
- /// <param name="length">数值部分长度</param>
- /// <param name="clip">数值超长后是否截断</param>
- /// <returns>新编号</returns>
- private string GetNextCodeNo(string codeFormat, decimal number, int length, bool clip = false)
- {
- string no = number.ToString(string.Empty.PadLeft(length, Constant.C_0));
- if (clip && no.Length > length)
- {
- no = no.Substring(no.Length - length, length);
- }
- return string.Format(codeFormat, no);
- }
- #endregion
- #region 静态方法
- /// <summary>
- /// 获取编号格式
- /// </summary>
- /// <param name="codeNo">编号</param>
- /// <param name="codeRegular">编号正则表达式</param>
- /// <param name="codeFormat">编号格式化</param>
- /// <param name="number">数值部分</param>
- /// <param name="length">数值部分长度</param>
- /// <returns>编号格式是否正确</returns>
- private static bool GetMatchCodeNo(string codeNo, string codeRegular,
- out string codeFormat, out decimal number, out int length)
- {
- codeFormat = codeNo;
- number = 0;
- length = 0;
- bool result = false;
- if (string.IsNullOrWhiteSpace(codeNo) ||
- string.IsNullOrWhiteSpace(codeRegular) ||
- !codeRegular.Contains("?<no>"))
- {
- return result;
- }
- Match match = Regex.Match(codeNo, codeRegular);
- if (match.Success)
- {
- StringBuilder sb = new StringBuilder();
- Group noGroup = match.Groups["no"];
- if (noGroup.Success)
- {
- string no = noGroup.Value;
- if (decimal.TryParse(no, out number))
- {
- result = true;
- int index = noGroup.Index;
- length = noGroup.Length;
- sb.Append(codeNo.Substring(0, index));
- sb.Append("{0}");
- sb.Append(codeNo.Substring(index + length));
- codeFormat = sb.ToString();
- }
- }
- }
- return result;
- }
- #endregion
- }
- }
|