using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Windows.Forms;
using
System.Reflection;
namespace
NClay.Windows
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public class ValidaterFactory
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public ValidaterFactory(ErrorProvider ep)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mErrorTip = ep;
}
private ErrorProvider mErrorTip;
private Dictionary<Control, ValidaterInfo> mValidaters = new Dictionary<Control, ValidaterInfo>();
public void AddValidater(Control control,IValidater validater,string errorTip)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Type objtype = control.GetType();
System.ComponentModel.DefaultBindingPropertyAttribute[] dpa
= (System.ComponentModel.DefaultBindingPropertyAttribute[])control.GetType().GetCustomAttributes(typeof(System.ComponentModel.DefaultBindingPropertyAttribute), true);
AddValidater(control, validater, errorTip, dpa[0].Name);
}
public void AddValidater(Control control, IValidater validater, string errorTip, string property)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ValidaterInfo info;
Type objtype = control.GetType();
PropertyInfo defproperty;
defproperty = objtype.GetProperty(property);
info = new ValidaterInfo(defproperty, validater, errorTip);
mValidaters.Add(control, info);
control.Validating += new System.ComponentModel.CancelEventHandler(onValidateing);
}
public bool IsVali()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mErrorTip.Clear();
bool isVali = true;
bool AllVali = true;
foreach (Control item in mValidaters.Keys)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ValidaterInfo info = mValidaters[item];
object value = info.Property.GetValue(item, null);
isVali = info.Validater.Validating(value);
if (!isVali)
mErrorTip.SetError(item, info.Message);
if (!isVali)
AllVali = isVali;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
return AllVali;
}
private void onValidateing(object source, System.ComponentModel.CancelEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ValidaterInfo info = mValidaters[(Control)source];
object value = info.Property.GetValue(source, null);
bool isVali = info.Validater.Validating(value);
if (!isVali)
mErrorTip.SetError((Control)source, info.Message);
else
mErrorTip.SetError((Control)source, null);
}
class ValidaterInfo
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public ValidaterInfo(PropertyInfo property, IValidater validater,
string message)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Property = property;
Message = message;
Validater = validater;
}
private PropertyInfo mProperty;
public PropertyInfo Property
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return mProperty;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mProperty = value;
}
}
private string mMessage;
public string Message
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return mMessage;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mMessage = value;
}
}
private IValidater mValidater;
public IValidater Validater
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return mValidater;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mValidater = value;
}
}
}
}
public interface IValidater
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
bool Validating(object value);
}
public abstract class ValidaterBase:IValidater
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
IValidater 成员#region IValidater 成员
public abstract bool Validating(object value);
protected T CastValue<T>(object value)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (value is IConvertible)
return (T)System.Convert.ChangeType(value, typeof(T));
return (T)value;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
}
public class StringValidater:ValidaterBase
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public StringValidater(int min, int max)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
LengthMin = min;
LengthMax = max;
}
public StringValidater(string regex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
RegexString = regex;
}
private bool mNonNull = false;
public bool NonNull
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return mNonNull;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mNonNull = value;
}
}
private int mLengthMin = int.MinValue;
public int LengthMin
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return mLengthMin;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mLengthMin = value;
}
}
private int mLengthMax = int.MinValue;
public int LengthMax
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return mLengthMax;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mLengthMax = value;
}
}
private string mRegexString = null;
public string RegexString
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return mRegexString;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mRegexString = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
IValidater 成员#region IValidater 成员
public override bool Validating(object value)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string newvalue= null;
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
newvalue = CastValue<string>(value);
}
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
if (NonNull)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (newvalue == null || newvalue == string.Empty)
return false;
}
if (LengthMin != int.MinValue)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (newvalue.Length < LengthMin)
return false;
}
if (LengthMax != int.MinValue)
if (newvalue.Length > LengthMax)
return false;
if (RegexString != null && RegexString != string.Empty)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (newvalue == null || newvalue == string.Empty)
return false;
return System.Text.RegularExpressions.Regex.IsMatch(newvalue, RegexString, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
}
return true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
}
public class SturctValidater<T> : ValidaterBase where T:struct,IComparable
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public SturctValidater(T min,T max)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Min = min;
Max = max;
}
private T mMax = default(T);
public T Max
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return mMax;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mMax = value;
}
}
private T mMin = default(T);
public T Min
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return mMin;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mMin = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
IValidater 成员#region IValidater 成员
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override bool Validating(object value)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
T newvalue;
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
newvalue = CastValue<T>(value);
}
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (Min.CompareTo(default(T))>0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (newvalue.CompareTo(Min) <0)
return false;
}
if (Max.CompareTo(default(T)) >0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (newvalue.CompareTo(Max)>0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
}
return true;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
}
}
使用代码
private void button1_Click_1(object sender, EventArgs e)
{
if (mVF.IsVali())
{
MessageBox.Show("ok");
}
}
NClay.Windows.ValidaterFactory mVF;
private void Form1_Load(object sender, EventArgs e)
{
mVF = new NClay.Windows.ValidaterFactory(errorProvider1);
mVF.AddValidater(textBox1, new NClay.Windows.StringValidater(5 , 10), "请输入长度为5-10的字符串!");
}