using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace TaskManager
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public static class ValidateHelper
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
static ValidateHelper()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
IsErr = false;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static bool IsErr
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get;
set;
}
static ErrorProvider ep = new ErrorProvider();
public static bool Validate(this TextBox ctrl, System.Func<TextBox, bool> func, string errmsg)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!func(ctrl))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ep.SetError(ctrl, errmsg);
ctrl.Focus();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return !(IsErr=true);
}
return true;
}
public static bool Validate(this ListBox ctrl, System.Func<ListBox, bool> func, string errmsg)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!func(ctrl))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ep.SetError(ctrl, errmsg);
ctrl.Focus();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return !(IsErr = true);
}
return true;
}
public static void Clear()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ep.Clear();
IsErr = false;
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
还是照样上两张图
![](https://images.cnblogs.com/cnblogs_com/lexus/2008-08-23_103539.jpg)
用法
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
ValidateHelper.Clear();
this.txtName.Validate(t => string.IsNullOrEmpty(t.Text), "不能为空");
this.lstboxFileList.Validate(t => t.Items.Count < 2, "请加载任务资源包");
if (ValidateHelper.IsErr)
return;
//do sth ha
相关文章链接:
C#学习使用ErrorProvider
http://www.cnblogs.com/sondy/archive/2006/04/16/376254.html
ErrorProvider的用法
http://www.cnblogs.com/sondy/archive/2006/04/16/376254.html
验证能有多优雅
http://www.cnblogs.com/GrayZhang/archive/2008/09/01/1281526.html