C wpf textbox validating

by  |  22-May-2017 19:08

Anyone who has ever developed data driven applications of any sort has had to deal with validation.Let’s face it, users make mistakes, even if they are developers!Here you can see some useful techniques that accept only numbers in the textbox.

c wpf textbox validating-30

Let say you want to have a Text Box in which you only want to allow integers (0-9) or maybe you only want to allow strings, A-Za-z. This way it is already marked as handled and will not be added to the Text Box. You realize that there are two sets of numbers on a keyboard right? Well, of the seven requirements we need to handle, we only handled one.

Well, lets play around with this for a second and see what we can do. Key just happens to be an enum, which means they can basically be treated as integers. Here is a simple function to allow only natural numbers or number keys 0-9 in a Text Box. You have numbers in row above your QWERTY keys and you likely have a Number pad on the right of your keyboard as well. For an application to be considered release quality or enterprise ready or stable, all seven of these should be handled. Yes, I can handle both at the same time with a new event Text Changed.

I’m not going to harp too much on the details presented in these other articles.

Instead, I’m going to layout what I did to get this working and hopefully give you enough information to get started.

Well, we need to run the same function as for Drag and Drop, so to not duplicate code, it is time to create a class or object. Lets create a class called Number Text Box and it can do everything automagically.

Community Discussion