C#验证winforms上文本框的输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8915151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Validating input for textbox on winforms
提问by Rocshy
I want to check what the user is writing in a textbox before I save it in a database. What is the best way to do this? I guess I can always write some ifs or some try-catch blocks, but I was wondering if there's a better method. I've read something about Validating Events, but I am not sure how can I use them.
我想先检查用户在文本框中写入的内容,然后再将其保存到数据库中。做这个的最好方式是什么?我想我总是可以写一些 ifs 或一些 try-catch 块,但我想知道是否有更好的方法。我读过一些关于验证事件的内容,但我不确定如何使用它们。
采纳答案by dknaack
Description
描述
There are many ways to validate your TextBox. You can do this on every keystroke, at a later time, or on the Validatingevent.
有多种方法可以验证您的 TextBox。您可以在每次击键时、稍后或在Validating事件中执行此操作。
The Validatingevent gets fired if your TextBox looses focus. When the user clicks on a other Control, for example. If your set e.Cancel = truethe TextBox doesn't lose the focus.
Validating如果您的 TextBox 失去焦点,则会触发该事件。例如,当用户单击其他控件时。如果您设置e.Cancel = trueTextBox 不会失去焦点。
MSDN - Control.Validating EventWhen you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl.ActiveControl property to the current form, focus events occur in the following order
Enter
GotFocus
Leave
Validating
Validated
LostFocus
When you change the focus by using the mouse or by calling the Focus method, focus events occur in the following order:
Enter
GotFocus
LostFocus
Leave
Validating
Validated
MSDN - Control.Validating 事件当您通过使用键盘(TAB、SHIFT+TAB 等)、通过调用 Select 或 SelectNextControl 方法或通过将 ContainerControl.ActiveControl 属性设置为当前窗体来更改焦点时,焦点事件按以下顺序发生
进入
获得焦点
离开
证实
已验证
失去焦点
当您使用鼠标或通过调用 Focus 方法更改焦点时,焦点事件按以下顺序发生:
进入
获得焦点
失去焦点
离开
证实
已验证
Sample Validating Event
示例验证事件
private void textBox1_Validating(object sender, CancelEventArgs e)
{
if (textBox1.Text != "something")
e.Cancel = true;
}
Update
更新
You can use the ErrorProviderto visualize that your TextBox is not valid.
Check out Using Error Provider Control in Windows Forms and C#
您可以使用ErrorProvider来可视化您的 TextBox 无效。查看在 Windows 窗体和 C# 中使用错误提供程序控件
More Information
更多信息
回答by Chris Taylor
With WinForms you can use the ErrorProvider in conjunction with the Validatingevent to handle the validation of user input. The Validatingevent provides the hook to perform the validation and ErrorProvider gives a nice consistent approach to providing the user with feedback on any error conditions.
使用 WinForms,您可以将 ErrorProvider 与Validating事件结合使用来处理用户输入的验证。该Validating事件提供了执行验证的钩子,而 ErrorProvider 提供了一种很好的一致方法来为用户提供有关任何错误情况的反馈。
http://msdn.microsoft.com/en-us/library/system.windows.forms.errorprovider.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.errorprovider.aspx

