|
-
May 6th, 2008, 12:11 AM
#1
Thread Starter
Addicted Member
keypress in C#
here's my code:
Code:
private void keypress_charOnly(object sender, KeyPressEventArgs e)
{
int ascii = Convert.ToInt16(e.KeyChar);
if ((ascii >= 97 && ascii <= 122) || (ascii == 8) || (ascii >= 65 && ascii <= 90))
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
my question is, how can i prevent user to input integer values.
copy and paste (using mouse) can bypass the keypress event..
thanks.
Last edited by hoobas20; May 6th, 2008 at 01:01 AM.
-
May 6th, 2008, 12:29 AM
#2
Re: keypress in C#
csharp Code:
char keyChar = e.KeyChar; e.Handled == char.IsDigit(keyChar);
If the character is a digit then e.Handled is true and the character is rejected. Just note that that won't prevent digits being pasted in via the clipboard.
-
May 6th, 2008, 12:38 AM
#3
Thread Starter
Addicted Member
Re: keypress in C#
What event will I going to paste that code?
-
May 6th, 2008, 12:40 AM
#4
Thread Starter
Addicted Member
Re: keypress in C#
I Got it!
Code:
private void textBox1_TextChanged(object sender, EventArgs e)
{
bool isNum;
int num;
isNum = Int32.TryParse(textBox1.Text, out num);
if (!isNum)
{
MessageBox.Show("not numeric");
textBox1.Text = "0";
textBox1.SelectionStart = 0;
textBox1.SelectionLength = textBox1.Text.Length;
}
}
Thanks for replying!
-
May 6th, 2008, 12:47 AM
#5
Re: [RESOLVED]keypress in C#
The "proper" way to do this would be to handle the KeyPress event to prevent invalid characters being typed in and then override the WndProc method to intercept WM_PASTE (I think) messages to prevent invalid data being pasted in. What you've done will do a job but it's a bit of a hack, plus it could get rather annoying to the user if they type 10 valid characters and then accidentally type an invalid one and you wipe out the entire text. It also means that the user can't clear the TextBox and start typing because your code will stick a zero in there.
-
May 6th, 2008, 12:56 AM
#6
Thread Starter
Addicted Member
Re: [RESOLVED]keypress in C#
Sir, I want to adapt your code but. I don't know how to use that. I get some error.
Code:
private void keypress_charOnly(object sender, KeyPressEventArgs e)
{
char keyChar = e.KeyChar;
e.Handled == char.IsDigit(keyChar);
}
Is this the proper way in doing that? Sorry again.. I am new in C#..
-
May 6th, 2008, 01:04 AM
#7
Re: keypress in C#
First up, in your original post you said:
my question is, how can i prevent user to input integer values.
That says that you don't want the user to enter any numbers. I'm guessing now that that's just the language barrier and what you actually want is for the user to enter nothing but numbers. Is that correct? If so then the code would be:
csharp Code:
char keyChar = e.KeyChar; e.Handled == !char.IsControl(keyChar) && !char.IsDigit(keyChar);
in the KeyPress event handler.
-
May 6th, 2008, 01:10 AM
#8
Thread Starter
Addicted Member
-
May 6th, 2008, 01:34 AM
#9
Frenzied Member
Re: keypress in C#
shouldn't that be 1 = not 2?
-
May 6th, 2008, 01:39 AM
#10
Re: keypress in C#
 Originally Posted by high6
shouldn't that be 1 = not 2?
Absolutely. Sorry about that.
csharp Code:
e.Handled = !char.IsControl(keyChar) && !char.IsDigit(keyChar);
Not quite sure why I did that. Stupidity maybe?
-
May 6th, 2008, 01:46 AM
#11
Thread Starter
Addicted Member
Re: keypress in C#
It still bypass the copy paste. what should i do?
-
May 7th, 2008, 12:37 AM
#12
Hyperactive Member
Re: keypress in C#
Here's another way to do this, which is more processor intensive, but is much less annoying.
Code:
private void textBox1_TextChanged(object sender, EventArgs e)
{
foreach (char i in textBox1.Text)
{
if (!char.IsDigit(i))
{
textBox1.Text = textBox1.Text.Replace(i.ToString(), "");
}
}
}
The only problem with it is that it returns the text curser to the front of the text box every time you input an invalid character. Remove the "!" if you want only text, leave it there if you want only numbers.
Cheers,
Qu.
"Why do all my attempts at science end with me getting punched by batman?" xkcd.
| Pong| |
Sorry for not posting more often.
-
Mar 3rd, 2010, 01:32 AM
#13
New Member
Re: keypress in C#
Thanks !
However, this does not allow entering "."
I need to allow the user to enter a value like "3.25"
How do I do this ?
-
Mar 3rd, 2010, 01:43 AM
#14
Re: keypress in C#
 Originally Posted by acpt
Thanks !
However, this does not allow entering "."
I need to allow the user to enter a value like "3.25"
How do I do this ?
Post #10 shows how to combine multiple conditions. You simply need to add another condition that checks whether the KeyChar is a decimal point.
-
Mar 6th, 2010, 10:05 AM
#15
Re: keypress in C#
Hey,
If you are wanting a control that only accepts numbers, why not simply use a NumericUpDown control?
Otherwise, have a look at this thread here:
http://www.vbforums.com/showthread.php?t=583495
Gary
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
|