validation of input for text box

G

gordon

Hello

I am just starting off with C# after a long time out of programming.

I am developing some test applications for pocket pcs that use the input to
text boxes as the source for an equation. However, I cant find any
'out-of-the-box' validation methods to ensure that only numbers are input or
vice versa to ensure that only characters are input for other fields

Any code that achieves this would be appreciated.

My text box is caled tbInput and I want to validate it immediately after
the focus has been lost from that control and to show a message box
informing the user of the error.

thanks

Doug
 
G

Guest

I am developing some test applications for pocket pcs that use the input to
text boxes as the source for an equation. However, I cant find any
'out-of-the-box' validation methods to ensure that only numbers are input or
vice versa to ensure that only characters are input for other fields

Any code that achieves this would be appreciated.

Make a KeyPress event handler for the TextBox and tell the system to ignore
invalid characters:


private void tbCab_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
if ( ( ( e.KeyChar < '0' ) || ( e.KeyChar > '9' ) ) && (
e.KeyChar != '\b' ) )
{
e.Handled = true ;
}
}
 

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

Similar Threads

Validation rule for Combo Box 0
web form input validation 4
Validation on text input boxes on a form 11
Input Box Q 3
Text box input??? 1
Validate text box 1
Validate User Input in Dialog box 3
TextBox validation. 2

Top