Results 1 to 15 of 15

Thread: keypress in C#

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    166

    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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: keypress in C#

    csharp Code:
    1. char keyChar = e.KeyChar;
    2.  
    3. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    166

    Re: keypress in C#

    What event will I going to paste that code?

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    166

    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!

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    166

    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#..

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. char keyChar = e.KeyChar;
    2.  
    3. e.Handled == !char.IsControl(keyChar) && !char.IsDigit(keyChar);
    in the KeyPress event handler.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    166

    Re: keypress in C#

    I still get an error.

    Name:  1.bmp
Views: 7697
Size:  219.6 KB

    Name:  2.bmp
Views: 7601
Size:  219.6 KB


  9. #9
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: keypress in C#

    shouldn't that be 1 = not 2?

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: keypress in C#

    Quote Originally Posted by high6
    shouldn't that be 1 = not 2?
    Absolutely. Sorry about that.
    csharp Code:
    1. e.Handled = !char.IsControl(keyChar) && !char.IsDigit(keyChar);
    Not quite sure why I did that. Stupidity maybe?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    166

    Re: keypress in C#

    It still bypass the copy paste. what should i do?

  12. #12
    Hyperactive Member Quasar6's Avatar
    Join Date
    Mar 2008
    Location
    Sol 3
    Posts
    325

    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.

  13. #13
    New Member
    Join Date
    Apr 2006
    Posts
    12

    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 ?

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: keypress in C#

    Quote Originally Posted by acpt View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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
  •  



Click Here to Expand Forum to Full Width