WinForm UI 验证 [英] WinForm UI Validation

查看:30
本文介绍了WinForm UI 验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的 winform 应用程序中实现输入验证.有许多不同的表单可以输入数据,我不想通过表单进行控制,而是为每个项目创建 isValid 等.其他人是如何处理这个问题的?

I need to implement input validation throughout my winform app. There are many different forms where data can be entered and I would like to not go control by control by form and create isValid etc per item. How have others dealt with this?

我看到大多数相关帖子都涉及 Web 应用程序和/或提到 企业库验证应用程序块.现在我承认我还没有彻底研究过 ELVAB,但它似乎对于我需要的东西来说太过分了.我目前的想法是编写一个具有各种要求的类库,并将其作为参数传递给它.我已经有一个 RegEx 函数库,用于诸如 isValidZipCode 之类的东西,所以这可能是我开始的地方.

I see that most related posts deal with Web Apps and/or mention Enterprise Library Validation Application Block. Now I admit I haven't thoroughly researched ELVAB but it seems like overkill for what I need. My current thought is to write a class library with the various requirements and pass it a control as a parameter. I already have a Library of RegEx functions for things like isValidZipCode and such so that may be a place for me to start.

我想要的是一个 Validate 按钮,它 onClick 循环浏览该表单页面上的所有控件并执行所需的验证.我怎样才能做到这一点?

What I would like to have is a Validate button that onClick cycles through all the controls on that Form Page and performs the needed validation. How can I accomplish this?

推荐答案

在我自己的应用程序中,我需要在输入尺寸时对其​​进行验证.我使用的顺序如下

In my own application I need to validate dimensions as they are typed in. The sequence I used is as follows

  1. 用户选择或输入然后移动远离控制.
  2. 控件失去焦点并通知发送它的 ID 的视图和输入文本.
  3. 视图检查什么形状程序(实现接口的类)创建了表单并将其传递给ID 和输入文本
  4. 形状程序返回一个响应.
  5. 如果响应正常,则查看更新形状的正确条目班级.
  6. 如果响应正常,视图会告诉通过一个接口的形式,它可以将焦点转移到下一个条目.
  7. 如果响应不正确,则视图查看响应并使用表单接口告诉表单什么去做.这通常意味着焦点移回违规条目显示一条消息告诉用户发生了什么.

这种方法的优势在于,对于给定的形状程序,验证集中在一个位置.我不必去修改每个控件,甚至不必担心表单上的不同类型的控件.早在我设计软件时,我就决定了 UI 将如何用于文本框、列表框、组合框等.此外,不同严重程度的处理方式也不同.

The advantage of this approach that validation is centralized in one location for a given Shape Program. I don't have to go modify each control or even really worry about the different types of controls on the form. Way back when I designed the software I decided how the UI going to work for textboxes, listboxes, combo boxes, etc. Also different levels of severity is handled differently.

视图负责指示表单通过接口做什么.它的实际实现方式由表单本身在接口的实现中处理.视图不关心表单是否显示黄色警告和红色错误.只是它处理这两个级别.稍后,如果出现显示警告与错误的更好主意,我可以在表单本身中进行更改,而不是使用视图逻辑或形状程序中的验证.

The View takes care of that instructing the Form what to do through the Interface. How it actually is implemented is handled by the Form itself in it's implementation of the Interface. The View doesn't care if the Form is displaying yellow for warning and red for error. Only that it handles those two levels. Later if a better idea of displaying warning vs errors comes along I can make the change in the Form itself rather mucking around with the View logic or the validate in Shape Program.

如果您正在考虑创建一个类来保存验证逻辑,那么您已经成功了一半,这将使您在新设计中完成剩下的工作.

You are already halfway there if you are considering making a class to hold your validation logic this will get you the rest of the way in your new design.

这篇关于WinForm UI 验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆