WinForm UI Validation
来源:互联网 发布:卡夫亨氏的管培生 知乎 编辑:程序博客网 时间:2024/06/03 14:08
1) Just copy and paste this section of code to your project. We're using Reflection so you need to add System.Refelection to your using statements
class Validation{
public static bool hasValidationErrors(System.Windows.Forms.Control.ControlCollection controls)
{
bool hasError = false;
// Now we need to loop through the controls and deterime if any of them have errors
foreach (Control controlin controls)
{
// check the control and see what it returns
bool validControl =IsValid(control);
// If it's not valid then set the flag and keep going. We want to get through all
// the validators so they will display on the screen if errorProviders were used.
if (!validControl)
hasError = true;
// If its a container control then it may have children that need to be checked
if (control.HasChildren)
{
if (hasValidationErrors(control.Controls))
hasError = true;
}
}
return hasError;
}
// Here, let's determine if the control has a validating method attached to it
// and if it does, let's execute it and return the result
private static boolIsValid(object eventSource)
{
string name ="EventValidating";
Type targetType = eventSource.GetType();
do
{
FieldInfo[] fields= targetType.GetFields(
BindingFlags.Static|
BindingFlags.Instance|
BindingFlags.NonPublic);
foreach (FieldInfo fieldin fields)
{
if (field.Name== name)
{
EventHandlerList eventHandlers= ((EventHandlerList)(eventSource.GetType().GetProperty("Events",
(BindingFlags.FlattenHierarchy|
(BindingFlags.NonPublic| BindingFlags.Instance))).GetValue(eventSource,null)));
Delegate d = eventHandlers[field.GetValue(eventSource)];
if ((!(d== null)))
{
Delegate[] subscribers= d.GetInvocationList();
// ok we found the validation event, let's get the event method and call it
foreach(Delegate d1in subscribers)
{
// create the parameters
object sender= eventSource;
CancelEventArgs eventArgs= newCancelEventArgs();
eventArgs.Cancel= false;
object[] parameters= newobject[2];
parameters[0]= sender;
parameters[1]= eventArgs;
// call the method
d1.DynamicInvoke(parameters);
// if the validation failed we need to return that failure
if (eventArgs.Cancel)
returnfalse;
else
returntrue;
}
}
}
}
targetType = targetType.BaseType;
} while(targetType !=null);
return true;
}
}
2) Use the standard Validating event on any control you want to validate. Be Sure to use e.Cancel when the validation fails!
private void txtLastName_Validating(object sender, CancelEventArgs e) { if (txtLastName.Text.Trim() == String.Empty) { errorProvider1.SetError(txtLastName, "Last Name is Required"); e.Cancel = true; } else errorProvider1.SetError(txtLastName, ""); }
3) Don't skip this step! Set the AutoValidate property on the form toEnableAllowFocusChange. This will allow tabbing to another control even when the validation fails.
4) Finally, in your Submit Button method, call the Validation method and specify what container you want to check. It can be the whole form, or just a container on the form like a Panel or a Group.
private void btnSubmit_Click(object sender, EventArgs e) { // the controls collection can be the whole form or just a panel or group if (Validation.hasValidationErrors(frmMain.Controls)) return; // if we get here the validation passed this.close(); } http://stackoverflow.com/questions/769184/winform-ui-validation
- WinForm UI Validation
- EF & Model Validation & WinForm UI DataBinding
- UI(WinForm)
- winform ui 设计
- winform ui 设计参考
- WinForm 多线程操作UI
- Validation
- validation
- Validation
- [WinForm]WinForm跨线程UI操作常用控件类大全
- WinForm UI设计与开发思路
- UI Automation Test(Winform,WPF)
- 用WebForm替换WinForm做UI
- WCF常见问题(2) -- Winform Host UI阻塞
- winform UI后台异步工作线程封装
- C#winform之跨线程UI调用
- winform异步线程更新UI问题
- Winform线程通信(异步更新UI--SynchronizationContext
- 利用maven组合SSH例子成功。
- (32)数组中常用的多遍历方式(33)PHP中预定义的超全局数组
- UML统一建模语言
- 理解WebKit和Chromium: Chromium多进程模型
- 在HTML中用Javascript和接收参数和JSP接受参数
- WinForm UI Validation
- Hash表查找
- android 客户端数据传递之一:Web Service 代码
- android 客户端数据传递之二:基于Http协议获取数据 代码
- Java程序员面试中的多线程问题
- spring使用注解@PostConstruct的xml配置
- 星级评分的简化实现
- 现代汉语语料库加工规范
- 如果构造器可以被继承····