在 C# 中按按钮进行表单验证的最简单方法? [英] Easiest way to do form validations on button press in C#?

查看:39
本文介绍了在 C# 中按按钮进行表单验证的最简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于 C# 的 WinForms 应用程序,它有很多菜单,每个菜单都有多个用于用户输入的控件..所有表单都有确定按钮,可以处理用户提供的输入..

例如,当用户按下 OK 按钮时,我必须对所有这些控件进行验证.要检查输入的字符串是否为数字、是否具有特定长度、是否存在等..

是否有任何 C# 功能可用于执行此操作,而不是我必须为每个确定"按钮编写完整的代码?

解决方案

一种方法是从 OK 按钮单击事件处理程序调用 ValidateValidateChildren.然后,您将为每个要验证的控件创建一个 Validating 事件处理程序.

请参阅这篇关于用户输入验证的 MSDN 文章.

I have a C# based WinForms application which has a lot of menus in it, each of which have multiple controls for user input .. All the forms have OK buttons which do processing on the inputs given by the user ..

I have to do validations on all these controls when the user presses the OK button for example .. To check if string entered is a number or not, is of a specific length or not, is present or not, etc ...

Is there any C# functionality available to do this, instead of me having to write full fledged code for each OK button ?

解决方案

One way to do this is to call Validate or ValidateChildren from the OK button click event handler. You would then have a Validating event handler for each control to be validated.

See this MSDN article on User Input Validation.

这篇关于在 C# 中按按钮进行表单验证的最简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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