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.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
With WinForms you can use the ErrorProvider in conjunction with the
Validating
event to handle the validation of user input. TheValidating
event provides the hook to perform the validation and ErrorProvider gives a nice consistent approach to providing the user with feedback on any error conditions.http://msdn.microsoft.com/en-us/library/system.windows.forms.errorprovider.aspx
Description
There are many ways to validate your TextBox. You can do this on every keystroke, at a later time, or on the
Validating
event.The
Validating
event gets fired if your TextBox looses focus. When the user clicks on a other Control, for example. If your sete.Cancel = true
the TextBox doesn't lose the focus.Sample Validating Event
Update
You can use the
ErrorProvider
to visualize that your TextBox is not valid. Check out Using Error Provider Control in Windows Forms and C#More Information