954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Is there a better way to check for key types?

I'm working on a custom textbox control that can "filter" out inappropriate input.

The filter property can be:

None,
Numerical,
LowerAlpha,
UpperAlpha,
Alpha,
AlphaNumeric,
Symbol

None would function like a normal textbox
Numerical would be any number key (1,2,3...)
LowerAlpha would be any alphabet character as long as it is lower cased
UpperAlpha would be any alphabet character as long as it is upper cased
Alpha is any alphabet character (case is ignored)
AlphaNumeric is any alphabet or number key
Symbol would be any symbol key (;,.'/?*&%!` etc...)

My question is, instead of on the KeyDown event checking which key was pressed by having if/then ad nauseum statements is there a system defined function or something to check these a bit better?

My check to see if a key was an integer is:

if (RestrictBy == Restrictions.Alpha)
                {
                    if (e.KeyCode > Keys.D0 || e.KeyCode < Keys.D9)
                    {
                        if (e.KeyCode > Keys.NumPad0 || e.KeyCode < Keys.NumPad9)
                        {

                        }
                    }
                }


I mean this is just getting out of control! An if statement for determining if a key was a alphabet or symbol key would be huge! There has to be a better way...or am I stuck?

zachattack05
Posting Pro
516 posts since Dec 2009
Reputation Points: 61
Solved Threads: 15
 

Use char type method.

1. char.IsDigit
2. char.isLower and many more.

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

Hmmmm I'll check into that!

Thanks!

zachattack05
Posting Pro
516 posts since Dec 2009
Reputation Points: 61
Solved Threads: 15
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You