|
-
November 6th, 2002, 12:23 PM
#1
preventing non-ints in a string
How do you do this in C#? I'm used to making an edit-box in VC, and forcing it to only accept int values. Can't seem to find the way to do this in C#.
Thanks,
sternaphile
"Judge a man by his questions rather than his answers." - Voltaire
-
November 6th, 2002, 01:56 PM
#2
Write a handler for KeyDown/KeyPress (not sure which, but one will work) in the text box. Check the value of the key. If not a number, stop processing i.e. .Handled()
Bill F
-
November 6th, 2002, 02:37 PM
#3
Sure, that would work, but how about something along the lines of isdigit()?
"Judge a man by his questions rather than his answers." - Voltaire
-
November 8th, 2002, 02:22 PM
#4
As in using isdigit() to check the key? The KeyPress handler will return the key pressed. I'm sure there is a static function Keys.IsDigit() or some sort that could do that.
Bill F
-
November 8th, 2002, 08:52 PM
#5
In your validation code(KeyPress or KeyDown), do the following:
Code:
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if ((Char.IsDigit(e.KeyChar) == false) && (Char.IsControl(e.KeyChar) == false))
{
e.Handled = true;
}
}
That chesk for digits(0-9) and control keys(arrows, delete, etc). Hope that helps!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|