最好使用 Validating 或 Leave 事件来验证文本框数据? [英] Better to use Validating or Leave event to validate textbox data?

查看:29
本文介绍了最好使用 Validating 或 Leave 事件来验证文本框数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在为 VB .NET 文本框编码验证逻辑时,哪个事件处理程序更适合使用:Validating 还是 Leave?

When coding validation logic for a VB .NET textbox, which event handler is better to use: Validating or Leave?

据我所知,它们同时发生.但是,根据这篇文章:MSDN:Control.Leave 事件,离开事件发生在验证事件之前.这最初会让我觉得我宁愿使用 Leave 事件,因为它首先发生.

From what I understand, they both occur at the same time. However, according to this article: MSDN: Control.Leave Event, the Leave event occurs right before the validating event. This would initially make me think I would rather use the Leave event, as it occurs first.

但是,为了代码可读性,将所有验证代码放在 Validating 事件中是有意义的.

However, for code readability, it would make sense to place all validation code in the Validating event.

那么,就效率和行业标准而言,哪个是更好的选择?

So, which is the better option, in terms of both efficiency and industry-standards?

推荐答案

你应该总是使用 Validating 事件,它被用来支持验证.如果不是为了防止焦点更改,那么至少对于 CausesValidation 属性.例如,在对话框的取消"按钮上将其设置为 False.当用户决定关闭对话框时,验证任何内容都没有意义.

You should always use the Validating event, it was made to support validation. If not to prevent the focus change then at least for the CausesValidation property. Which you set to False on, say, the Cancel button of a dialog. No point in validating anything when the user decides to dismiss the dialog.

这篇关于最好使用 Validating 或 Leave 事件来验证文本框数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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