C#控件输入限制通用类
我们在程序设计中经常会遇到需要限制控件输入的情况,比如金额只能是数值,不能是其他的类型等等,像这样的验证有许多许多,所以为了更好的维护和更快的编写代码,我对验证进行了一些封装。
下面就是一些简单的代码了:
using System.Text.RegularExpressions; namespace Trevan.Helper.Algorithm { /// <summary> /// 用来判断字符串是否符合规则 /// </summary> public static class ContentJudge { /// <summary> /// 用来判断字符串是否是正整数 /// </summary> /// <param name="content">欲判断的字符串</param> /// <returns>返回判断结果bool,合法为true</returns> public static bool JudgeIsPositiveInt(string content) { return Regex.IsMatch(content, @"^\d+$"); } /// <summary> /// 用来判断字符串是否是正浮点数 /// </summary> /// <param name="content">欲判断的字符串</param> /// <returns>返回判断结果bool,合法为true</returns> public static bool JudgeIsPositiveDecimal(string content) { return Regex.IsMatch(content, @"^\d+[\.]{1}\d+$"); } /// <summary> /// 判断是否为负整数 /// </summary> /// <param name="content">欲判断的字符串</param> /// <returns>返回判断结果bool,合法为true</returns> public static bool JudgeIsMinusInt(string content) { return Regex.IsMatch(content, @"^-\d+$"); } /// <summary> /// 用来判断字符串是否是负浮点数 /// </summary> /// <param name="content">欲判断的字符串</param> /// <returns>返回判断结果bool,合法为true</returns> public static bool JudgeIsMinusDecimal(string content) { return Regex.IsMatch(content, @"^-\d+[\.]{1}\d+$"); } /// <summary> /// 判断是否为汉字 /// </summary> /// <param name="content">欲判断的字符串</param> /// <returns>返回判断结果bool,合法为true</returns> public static bool JudgeIsChinese(string content) { return Regex.IsMatch(content, @"^[\u4e00-\u9fa5]+$"); } /// <summary> /// 判断是否为大写字母 /// </summary> /// <param name="content">欲判断的字符串</param> /// <returns>返回判断结果bool,合法为true</returns> public static bool JudgeIsCapital(string content) { return Regex.IsMatch(content, @"^[A-Z]+$"); } /// <summary> /// 判断是否为小写字母 /// </summary> /// <param name="content">欲判断的字符串</param> /// <returns>返回判断结果bool,合法为true</returns> public static bool JudgeIsSmallLetter(string content) { return Regex.IsMatch(content, @"^[a-z]+$"); } /// <summary> /// 判断是否为字母 /// </summary> /// <param name="content">欲判断的字符串</param> /// <returns>返回判断结果bool,合法为true</returns> public static bool JudgeIsLetter(string content) { return Regex.IsMatch(content, @"^[A-ZA-Z]+$"); } /// <summary> /// 判断是否为字母和数字混合 /// </summary> /// <param name="content">欲判断的字符串</param> /// <returns>返回判断结果bool,合法为true</returns> public static bool JudgeIsLetterAndNumber(string content) { return Regex.IsMatch(content, @"^[A-Za-z0-9]+$"); } /// <summary> /// 判断是否为字母和中文混合 /// </summary> /// <param name="content">欲判断的字符串</param> /// <returns>返回判断结果bool,合法为true</returns> public static bool JudgeIsLetterAndChinese(string content) { return Regex.IsMatch(content, @"^[A-Za-z\u4e00-\u9fa5]+$"); } /// <summary> /// 判断是否为数字和中文混合 /// </summary> /// <param name="content">欲判断的字符串</param> /// <returns>返回判断结果bool,合法为true</returns> public static bool JudgeIsNumberAndChinese(string content) { return Regex.IsMatch(content, @"^[0-9\u4e00-\u9fa5]+$"); } /// <summary> /// 判断是否为E-Mail地址 /// </summary> /// <param name="content">欲判断的字符串</param> /// <returns>返回判断结果bool,合法为true</returns> public static bool JudgeIsMailAddress(string content) { return Regex.IsMatch(content, @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"); } /// <summary> /// 判断是否为手机号码 /// </summary> /// <param name="content">欲判断的字符串</param> /// <returns>返回判断结果bool,合法为true</returns> public static bool JudgeIsMobilePhoneNumber(string content) { return Regex.IsMatch(content, @"^1[3,5,8][0-9])\d{8}$"); } /// <summary> /// 判断是否为电话号码 /// </summary> /// <param name="content">欲判断的字符串</param> /// <returns>返回判断结果bool,合法为true</returns> public static bool JudgeIsPhoneNumber(string content) { return Regex.IsMatch(content, @"^(\d{3,4}-)?\d{6,8}$"); } /// <summary> /// 判断是否为IP /// </summary> /// <param name="content">欲判断的字符串</param> /// <returns>返回判断结果bool,合法为true</returns> public static bool JudgeIsIp(string content) { return Regex.IsMatch(content, @"^(\d{1,2}|1\d\d|2[0-4]d|25[0-5]).(\d{1,2}|1\d\d|2[0-4]d|25[0-5]).(\d{1,2}|1\d\d|2[0-4]d|25[0-5]).(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$"); } /// <summary> /// 自定义判断 /// </summary> /// <param name="content">欲判断的字符串</param> /// <param name="regex">正则表达式规则</param> /// <returns>返回判断结果bool,合法为true</returns> public static bool JudgeDiy(string content, string regex) { return Regex.IsMatch(content, regex); } } public enum ContentJudgeEnum { JudgeIsPositiveInt=1, JudgeIsPositiveDecimal=2, JudgeIsMinusInt=3, JudgeIsMinusDecimal=4, JudgeIsChinese=5, JudgeIsCapital=6, JudgeIsSmallLetter=7, JudgeIsLetter=8, JudgeIsLetterAndNumber=9, JudgeIsLetterAndChinese=10, JudgeIsNumberAndChinese=11, JudgeIsMailAddress=12, JudgeIsMobilePhoneNumber=13, JudgeIsPhoneNumber=14, JudgeIsIp=15, JudgeDiy=16, None=1000 }
}
using System.Drawing; using System.Globalization; using Trevan.Helper.Algorithm; using System; using System.Windows.Forms; namespace Trevan.Helper.WinForm.Control { /// <summary> /// 用于限制控件输入 /// </summary> public class EnterRestriction { public ContentJudgeEnum ContentJudgeE { get; set; } public string Expression { get; set; } private System.Windows.Forms.Control Control { get; set; } private static Color _color; public bool IsPass; public EnterRestriction(System.Windows.Forms.Control control, ContentJudgeEnum cje=ContentJudgeEnum.JudgeIsChinese) { /// Try
ContentJudgeE = cje; Control = control; control.KeyPress += control_KeyPress; control.Leave+=control_Leave; } void control_Leave(object sender, EventArgs e) {
/// Try
var content = Control.Text; if (content.Trim() == string.Empty||ContentJudgeE == ContentJudgeEnum.None) return; if (_color.IsEmpty) { _color = Control.BackColor; } else { Control.BackColor = _color; } IsPass = true; switch (ContentJudgeE) { case ContentJudgeEnum.JudgeDiy: if (ContentJudge.JudgeDiy(content, Expression)) return; break; case ContentJudgeEnum.JudgeIsIp: if (ContentJudge.JudgeIsIp(content)) return; break; case ContentJudgeEnum.JudgeIsMailAddress: if (ContentJudge.JudgeIsMailAddress(content)) return; break; case ContentJudgeEnum.JudgeIsMinusDecimal: if (ContentJudge.JudgeIsMinusDecimal(content)) return; break; case ContentJudgeEnum.JudgeIsMinusInt: if (ContentJudge.JudgeIsMinusInt(content)) return; break; case ContentJudgeEnum.JudgeIsMobilePhoneNumber: if (ContentJudge.JudgeIsMobilePhoneNumber(content)) return; break; case ContentJudgeEnum.JudgeIsCapital: if (ContentJudge.JudgeIsCapital(content)) return; break; case ContentJudgeEnum.JudgeIsChinese: if (ContentJudge.JudgeIsChinese(content)) return; break; case ContentJudgeEnum.JudgeIsLetter: if (ContentJudge.JudgeIsLetter(content)) return; break; case ContentJudgeEnum.JudgeIsLetterAndChinese: if (ContentJudge.JudgeIsLetterAndChinese(content)) return; break; case ContentJudgeEnum.JudgeIsLetterAndNumber: if (ContentJudge.JudgeIsLetterAndNumber(content)) return; break; case ContentJudgeEnum.JudgeIsNumberAndChinese: if (ContentJudge.JudgeIsNumberAndChinese(content)) return; break; case ContentJudgeEnum.JudgeIsPhoneNumber: if (ContentJudge.JudgeIsPhoneNumber(content)) return; break; case ContentJudgeEnum.JudgeIsPositiveDecimal: if (ContentJudge.JudgeIsPositiveDecimal(content)) return; break; case ContentJudgeEnum.JudgeIsPositiveInt: if (ContentJudge.JudgeIsPositiveInt(content)) return; break; case ContentJudgeEnum.JudgeIsSmallLetter: if (ContentJudge.JudgeIsSmallLetter(content)) return; break; } Control.BackColor = Color.Aquamarine; IsPass = false; } void control_KeyPress(object sender, KeyPressEventArgs e) {
if (e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Enter) { return; } switch (ContentJudgeE) { case ContentJudgeEnum.JudgeIsCapital: e.Handled = !ContentJudge.JudgeIsCapital(e.KeyChar.ToString(CultureInfo.InvariantCulture)); break; case ContentJudgeEnum.JudgeIsChinese: e.Handled = !ContentJudge.JudgeIsChinese(e.KeyChar.ToString(CultureInfo.InvariantCulture)); break; case ContentJudgeEnum.JudgeIsLetter: e.Handled = !ContentJudge.JudgeIsLetter(e.KeyChar.ToString(CultureInfo.InvariantCulture)); break; case ContentJudgeEnum.JudgeIsLetterAndChinese: e.Handled = !ContentJudge.JudgeIsLetterAndChinese(e.KeyChar.ToString(CultureInfo.InvariantCulture)); break; case ContentJudgeEnum.JudgeIsLetterAndNumber: e.Handled = !ContentJudge.JudgeIsLetterAndNumber(e.KeyChar.ToString(CultureInfo.InvariantCulture)); break; case ContentJudgeEnum.JudgeIsNumberAndChinese: e.Handled = !ContentJudge.JudgeIsNumberAndChinese(e.KeyChar.ToString(CultureInfo.InvariantCulture)); break; case ContentJudgeEnum.JudgeIsPositiveDecimal: e.Handled = !ContentJudge.JudgeIsPositiveDecimal(e.KeyChar.ToString(CultureInfo.InvariantCulture)); break; case ContentJudgeEnum.JudgeIsPositiveInt: e.Handled = !ContentJudge.JudgeIsPositiveInt(e.KeyChar.ToString(CultureInfo.InvariantCulture)); break; case ContentJudgeEnum.JudgeIsSmallLetter: e.Handled = !ContentJudge.JudgeIsSmallLetter(e.KeyChar.ToString(CultureInfo.InvariantCulture)); break; } } } }
使用这个类的方式:
EnterRestriction _er = new EnterRestriction(txtContent);
_er.ContentJudgeE = ContentJudgeEnum.None;
其实到这里基本上就完成了,根据这个代码可以把功能加入自定义控件中!
这个必定还有许多可以改善的地方,如果有BUG,或更好的方式,请告诉我下哦!相互学习吧!
-----