using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Windows.Forms;
using
System.Reflection;
namespace
NClay.Windows
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public class ValidaterFactory
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public ValidaterFactory(ErrorProvider ep)
![ExpandedSubBlockStart.gif](/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)
![ExpandedSubBlockStart.gif](/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)
![ExpandedSubBlockStart.gif](/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()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mErrorTip.Clear();
bool isVali = true;
bool AllVali = true;
foreach (Control item in mValidaters.Keys)
![ExpandedSubBlockStart.gif](/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;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
return AllVali;
}
private void onValidateing(object source, System.ComponentModel.CancelEventArgs e)
![ExpandedSubBlockStart.gif](/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
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public ValidaterInfo(PropertyInfo property, IValidater validater,
string message)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Property = property;
Message = message;
Validater = validater;
}
private PropertyInfo mProperty;
public PropertyInfo Property
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return mProperty;
}
set
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mProperty = value;
}
}
private string mMessage;
public string Message
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return mMessage;
}
set
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mMessage = value;
}
}
private IValidater mValidater;
public IValidater Validater
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return mValidater;
}
set
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mValidater = value;
}
}
}
}
public interface IValidater
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
bool Validating(object value);
}
public abstract class ValidaterBase:IValidater
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
IValidater 成员IValidater 成员#region IValidater 成员
public abstract bool Validating(object value);
protected T CastValue<T>(object value)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (value is IConvertible)
return (T)System.Convert.ChangeType(value, typeof(T));
return (T)value;
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
#endregion
}
public class StringValidater:ValidaterBase
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public StringValidater(int min, int max)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
LengthMin = min;
LengthMax = max;
}
public StringValidater(string regex)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
RegexString = regex;
}
private bool mNonNull = false;
public bool NonNull
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return mNonNull;
}
set
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mNonNull = value;
}
}
private int mLengthMin = int.MinValue;
public int LengthMin
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return mLengthMin;
}
set
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mLengthMin = value;
}
}
private int mLengthMax = int.MinValue;
public int LengthMax
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return mLengthMax;
}
set
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mLengthMax = value;
}
}
private string mRegexString = null;
public string RegexString
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return mRegexString;
}
set
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mRegexString = value;
}
}
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
IValidater 成员IValidater 成员#region IValidater 成员
public override bool Validating(object value)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string newvalue= null;
try
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
newvalue = CastValue<string>(value);
}
catch
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
if (NonNull)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (newvalue == null || newvalue == string.Empty)
return false;
}
if (LengthMin != int.MinValue)
![ExpandedSubBlockStart.gif](/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)
![ExpandedSubBlockStart.gif](/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;
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
#endregion
}
public class SturctValidater<T> : ValidaterBase where T:struct,IComparable
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public SturctValidater(T min,T max)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Min = min;
Max = max;
}
private T mMax = default(T);
public T Max
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return mMax;
}
set
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mMax = value;
}
}
private T mMin = default(T);
public T Min
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return mMin;
}
set
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mMin = value;
}
}
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
IValidater 成员IValidater 成员#region IValidater 成员
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public override bool Validating(object value)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
T newvalue;
try
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
newvalue = CastValue<T>(value);
}
catch
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
if (Min.CompareTo(default(T))>0)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (newvalue.CompareTo(Min) <0)
return false;
}
if (Max.CompareTo(default(T)) >0)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (newvalue.CompareTo(Max)>0)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
}
return true;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
#endregion
}
}
转载于:https://www.cnblogs.com/gjahead/archive/2007/07/30/836508.html