WinForm数据验证类 (转)

 

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.Windows.Forms;
None.gif
using  System.Reflection;
None.gif
namespace  NClay.Windows
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class ValidaterFactory
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public ValidaterFactory(ErrorProvider ep)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            mErrorTip 
= ep;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private ErrorProvider mErrorTip;
InBlock.gif        
private Dictionary<Control, ValidaterInfo> mValidaters = new Dictionary<Control, ValidaterInfo>();
InBlock.gif        
public void AddValidater(Control control,IValidater validater,string errorTip)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
InBlock.gif            Type objtype 
= control.GetType();
InBlock.gif           
InBlock.gif            System.ComponentModel.DefaultBindingPropertyAttribute[] dpa
InBlock.gif            
= (System.ComponentModel.DefaultBindingPropertyAttribute[])control.GetType().GetCustomAttributes(typeof(System.ComponentModel.DefaultBindingPropertyAttribute), true);
InBlock.gif            AddValidater(control, validater, errorTip, dpa[
0].Name);
InBlock.gif      
InBlock.gif           
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void AddValidater(Control control, IValidater validater, string errorTip, string property)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ValidaterInfo info;
InBlock.gif            Type objtype 
= control.GetType();
InBlock.gif            PropertyInfo defproperty;
InBlock.gif            defproperty 
= objtype.GetProperty(property);
InBlock.gif            info 
= new ValidaterInfo(defproperty, validater, errorTip);
InBlock.gif            mValidaters.Add(control, info);
InBlock.gif            control.Validating 
+= new System.ComponentModel.CancelEventHandler(onValidateing);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public bool IsVali()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            mErrorTip.Clear();
InBlock.gif            
bool isVali = true;
InBlock.gif            
bool AllVali = true;
InBlock.gif            
foreach (Control item in mValidaters.Keys)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ValidaterInfo info 
= mValidaters[item];
InBlock.gif                
object value = info.Property.GetValue(item, null);
InBlock.gif                isVali 
= info.Validater.Validating(value);
InBlock.gif                
if (!isVali)
InBlock.gif                    mErrorTip.SetError(item, info.Message);
InBlock.gif                
if (!isVali)
InBlock.gif                    AllVali 
= isVali;
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return AllVali;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void onValidateing(object source, System.ComponentModel.CancelEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif           
InBlock.gif            ValidaterInfo info 
= mValidaters[(Control)source];
InBlock.gif            
object value = info.Property.GetValue(source, null);
InBlock.gif            
bool isVali = info.Validater.Validating(value);
InBlock.gif            
if (!isVali)
InBlock.gif                mErrorTip.SetError((Control)source, info.Message);
InBlock.gif            
else
InBlock.gif                mErrorTip.SetError((Control)source, 
null);
InBlock.gif            
InBlock.gif            
InBlock.gif                
InBlock.gif            
InBlock.gif           
ExpandedSubBlockEnd.gif        }

InBlock.gif        
class ValidaterInfo
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public ValidaterInfo(PropertyInfo property, IValidater validater,
InBlock.gif                
string message)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Property 
= property;
InBlock.gif                Message 
= message;
InBlock.gif                Validater 
= validater;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
private PropertyInfo mProperty;
InBlock.gif            
public PropertyInfo Property
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
get
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return mProperty;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
set
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    mProperty 
= value;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
private string mMessage;
InBlock.gif            
public string Message
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
get
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return mMessage;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
set
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    mMessage 
= value;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
private IValidater mValidater;
InBlock.gif            
public IValidater Validater
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
get
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return mValidater;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
set
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    mValidater 
= value;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
public interface IValidater
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
bool Validating(object value);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public abstract class ValidaterBase:IValidater
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        IValidater 成员
IValidater 成员#region IValidater 成员
InBlock.gif         
InBlock.gif        
public abstract bool Validating(object value);
InBlock.gif        
protected T CastValue<T>(object value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (value is IConvertible)
InBlock.gif                
return (T)System.Convert.ChangeType(value, typeof(T));
InBlock.gif            
return (T)value;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

InBlock.gif    
public class StringValidater:ValidaterBase
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif       
InBlock.gif        
public StringValidater(int min, int max)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            LengthMin 
= min;
InBlock.gif            LengthMax 
= max;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public StringValidater(string regex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            RegexString 
= regex;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private bool mNonNull = false;
InBlock.gif        
public bool NonNull
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return mNonNull;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                mNonNull 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
private int mLengthMin = int.MinValue;
InBlock.gif        
public int LengthMin
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return mLengthMin;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                mLengthMin 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
private int mLengthMax = int.MinValue;
InBlock.gif        
public int LengthMax
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return mLengthMax;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                mLengthMax 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
private string mRegexString = null;
InBlock.gif        
public string RegexString
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return mRegexString;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                mRegexString 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ContractedSubBlock.gifExpandedSubBlockStart.gifIValidater 成员
IValidater 成员#region IValidater 成员
InBlock.gif        
public override bool Validating(object value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string newvalue= null;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                newvalue 
= CastValue<string>(value);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (NonNull)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (newvalue == null || newvalue == string.Empty)
InBlock.gif                    
return false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (LengthMin != int.MinValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (newvalue.Length < LengthMin)
InBlock.gif                    
return false;
InBlock.gif                
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (LengthMax != int.MinValue)
InBlock.gif                
if (newvalue.Length > LengthMax)
InBlock.gif                    
return false;
InBlock.gif            
if (RegexString != null && RegexString != string.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (newvalue == null || newvalue == string.Empty)
InBlock.gif                    
return false;
InBlock.gif                
return System.Text.RegularExpressions.Regex.IsMatch(newvalue, RegexString, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return true;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

InBlock.gif    
public class SturctValidater<T> :  ValidaterBase where T:struct,IComparable
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public SturctValidater(T min,T max)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Min 
= min;
InBlock.gif            Max 
= max;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private T mMax = default(T);
InBlock.gif        
public T Max
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return mMax;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                mMax 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
private T mMin = default(T);
InBlock.gif        
public T Min
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return mMin;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                mMin 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ContractedSubBlock.gifExpandedSubBlockStart.gifIValidater 成员
IValidater 成员#region IValidater 成员
InBlock.gif
InBlock.gif        
public override  bool Validating(object value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            T newvalue;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                newvalue 
= CastValue<T>(value);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if (Min.CompareTo(default(T))>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (newvalue.CompareTo(Min) <0)
InBlock.gif                    
return false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (Max.CompareTo(default(T)) >0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (newvalue.CompareTo(Max)>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return false;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return true;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/gjahead/archive/2007/07/30/836508.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值