Determine if an input key is a number, letter or special character

M

moondaddy

Is there a simple way to determine if an input key is a number, letter or
special character? in other words, was the user inputting data and not
typing a backspace, shift, etc.

Thanks.
 
P

Peter Duniho

Is there a simple way to determine if an input key is a number, letter
or special character? in other words, was the user inputting data and
not typing a backspace, shift, etc.

In what context?
 
M

Morten Wennevik [C# MVP]

Hi moondaddy,

You aren't telling is what kind of input you are talking about, but the Char
class has a set of static methods for determining what type a given character
is. For instance System.Char.IsDigit(), ...IsLetter(), ...IsControl().

If you mean input keys as in KeyEvent... there is no way to determine if a
given Key is a digit, letter or special character. You have to set up a list
of keys for each type, like D0-D9 + NumPad0-NumPad9 would mean digit input.

To ignore invalid entries in a TextBox, handle the OnKeyPress event and
check the Char value. If not valid, set KeyPressEventArgs.Cancel to true.
 
M

moondaddy

Thanks Morten,

I was referring to the keys input as in what did the user type. Sorry for
being unclear and your response was helpful.

--
(e-mail address removed)

Morten Wennevik said:
Hi moondaddy,

You aren't telling is what kind of input you are talking about, but the
Char
class has a set of static methods for determining what type a given
character
is. For instance System.Char.IsDigit(), ...IsLetter(), ...IsControl().

If you mean input keys as in KeyEvent... there is no way to determine if a
given Key is a digit, letter or special character. You have to set up a
list
of keys for each type, like D0-D9 + NumPad0-NumPad9 would mean digit
input.

To ignore invalid entries in a TextBox, handle the OnKeyPress event and
check the Char value. If not valid, set KeyPressEventArgs.Cancel to true.

--
Happy Coding!
Morten Wennevik [C# MVP]


moondaddy said:
Is there a simple way to determine if an input key is a number, letter or
special character? in other words, was the user inputting data and not
typing a backspace, shift, etc.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top