WinForm的用户界面验证 [英] WinForm UI Validation

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

问题描述

我需要在我的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但它的似乎的像矫枉过正我需要什么。我现在的想法是写一个类库的各种要求,并通过它控制作为一个参数。我已经有正则表达式的功能库之类的东西的 isValidZip code 的并且这样使可能是我开始的地方。

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.

我想有一个验证按钮,通过所有控件的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. 如果响应正常的查看 Shape的更新正确输入 类。
  6. 如果响应是OK的查看通知 通过接口的形式,它 为确定将重点转移到下一个项目。
  7. 如果响应不正常,查看 着眼于该响应,并使用 接口形式告知的形式是什么 去做。这通常意味着焦点 转移回到问题的项 并显示一条消息,告诉 用户发生了什么事。
  1. The user selects or types then moves away from the control.
  2. The control loses focus and notifies the View sending it's ID and the entry text.
  3. The View checks what Shape Program (a class implementing a interface) created the Form and passes it the ID and entry text
  4. The Shape Program returns a response.
  5. If the Response is OK the View updates correct Entry of the Shape Class.
  6. If the Response is OK the View tells the Form through a Interface that it is OK to shift the focus to the next entry.
  7. If the Response is not OK, the View looks at the response and using the Form Interface tells the form what to do. This usually means the focus shifts back to the offending entry with a message displayed telling the user what happened.

这种方法的有效性都集中在一个位置,对于一个给定的形状程序的优势。我没有去修改每个控制,甚至真正担心的窗体上的不同类型的控件。遥想当年我设计,我决定如何在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.

视图注意到这一照顾指示办通过该接口是什么形式。由窗体本身在它的接口的实现是如何实际上它是实现处理。视图不关心,如果窗体是显示黄色表示警告,红色表示错误。仅其处理这两个级别。以后如果有更好的主意显示警告VS错误出现时,我可以做的本身,而与视图逻辑或形状程序的验证碴周围的形态的变化。

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的用户界面验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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