开发者

What is the best way to do validations in windows forms

开发者 https://www.devze.com 2023-01-30 03:34 出处:网络
What is the best way to do validation in a windows forms application? What is the easiest way? What 开发者_开发知识库is the most attractive (to the end user) way?

What is the best way to do validation in a windows forms application?

What is the easiest way?

What 开发者_开发知识库is the most attractive (to the end user) way?

Regards,

-Kushan-


Concerning UI validation, I have a set of control validators, and I just plug 'em in where I need them by assigning their control. You can show errors using ErrorProvider, all you need is encapsulated framework to automate things.

First there is the ValidatorBase class. Make it abstract and inherit the Component class so you can have design time support. Store a private instance of ErrorProvider here, and use something like Template Method pattern (create a Validate method, which in turn calls the protected abstract DoValidation method). In concrete implementations of the base class just override the DoValidation and put your logic here. You can have EmptyValidator (check if control's value isn't empty), RegexValidator (check controls value with some reg. expression), GroupValidator (do Validate on every ValidatorBase instance in some list), whatever you want.

In the base class, you can add things like design-time support for properties (the error message, icon, control to validate etc...)

EDIT1: Now, concerning validation other then in the UI, that is the domain of your business layer, and your rules. There are frameworks / patterns for those things too, but I think you are asking about the UI validation.

EDIT2: ASP.NET has a set of similar validators built-in, although with more functionalities (client side validation, etc...), but to be honest, I don't like them that much.

EDIT3: also check: Is there any validation control available in .net win forms like asp.net web form?


One interface you might consider looking at is IDataErrorInfo along with the ErrorProvider class. I've got an old blog post that provides a list of the DataBinding classes and interfaces that might help: Data Binding Classes, Interfaces, and Attributes in Windows Forms 2.0.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号