CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: help

  1. #1
    Join Date
    Aug 2006
    Location
    Karachi,Pakistan
    Posts
    9

    Post text box which takes integer input

    hi to all;
    plz help me i want to restrict the text box control to take only digit as input. i m using vs.net 2003 (c#).
    loking forward to u people reply.
    TC.
    Last edited by wise programmer; September 1st, 2006 at 09:39 AM.

  2. #2
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: help

    Take a look at How to: Create a Numeric Text Box .

    Furthermore, plz use meaningful titles to your thread. It will bring more audience to your problem.
    Last edited by Ejaz; August 31st, 2006 at 03:51 PM.

  3. #3
    Join Date
    Aug 2006
    Posts
    69

    Re: help

    Use the KeyDown event of the TextBox. It's important to use the KeyDown because it's EventHandler contains the SuppressKeyPress property which is how we ignore keys we don't want.

    Code:
        this.textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown);
    In the actual event handler, make sure that you allow not just digits, but others things like delete and backspace, so that the user can fix a typo. It would be wise to also allow arrows and shift keys to let them select their entry or move about in it. And, I'd suggest allowing the tab and enter keys.

    Here's an example:
    Code:
        void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
          if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9) {
            if (e.KeyCode != Keys.Delete && e.KeyCode != Keys.Back) {
              e.SuppressKeyPress = true;
            }
          }
        }

  4. #4
    Join Date
    Aug 2006
    Location
    Karachi,Pakistan
    Posts
    9

    Exclamation Re: help

    when i m compiiling this code the error message is displayed:
    " 'System.Windows.Forms.KeyEventArgs' does not contain a definition for 'SuppressKeyPress'".
    I have added the key down event of text box.

  5. #5
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: help

    thats because thats a 2.0 specific property.

    feel free to use this one: http://www.sanity-free.org/article3.html capture key down only takes care of the use case where the user acutally types the text in... pasting will still allow text. the sample I posted manages that as well.

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

Featured