By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,986 Members | 1,193 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,986 IT Pros & Developers. It's quick & easy.

validation of input for text box

P: n/a
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
May 17 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
> 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 ;
}
}
May 18 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.