using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TaskManager


{
public static class ValidateHelper

{
static ValidateHelper()

{
IsErr = false;
}

public static bool IsErr

{
get;
set;
}
static ErrorProvider ep = new ErrorProvider();
public static bool Validate(this TextBox ctrl, System.Func<TextBox, bool> func, string errmsg)

{
if (!func(ctrl))

{
ep.SetError(ctrl, errmsg);
ctrl.Focus();

return !(IsErr=true);
}
return true;
}
public static bool Validate(this ListBox ctrl, System.Func<ListBox, bool> func, string errmsg)

{
if (!func(ctrl))

{
ep.SetError(ctrl, errmsg);
ctrl.Focus();

return !(IsErr = true);
}
return true;
}
public static void Clear()

{
ep.Clear();
IsErr = false;
}
}
}

还是照样上两张图

用法

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