我认为文本更改事件不是设置用户名Password bools的适当方法(假设它们是bool)。
每次用户键入一个字符,该事件将触发每个按下的输入键的检查。我认为您最好对每个控件使用失去焦点的事件。您也可以在鼠标离开控件时设置焦点丢失。
If you want to do something when a control loses focus then you should handle the
离开
event, not the
失落的焦点
event. If you want to prevent a control losing focus based on its content then you should handle the
证实
event.
鉴于您有一个需要单击的按钮,这使我的方法更好,因为单击按钮时,这将使焦点集中在一个新控件上,从而使丢失焦点事件(如果它们实际上是焦点在控件上)会触发,这使我的方法比您正在使用什么。
If you're going to click the
Button
, I would think that doing the checking then would make more sense. The only reason I can think to do otherwise is if you want the
Button
to remain disabled until valid data is entered. In that case,
TextChanged
would be more appropriate than
离开
, because you couldn't click a disabled
Button
to raise the event. That is not what's happening here though. In the vast majority of cases, testing a user name and password will be done against a database when the user clicks a
Button
, so that's really how it ought to be done here too. This is a contrived scenario but at least it should simulate a real scenario as closely as possible.