CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2001
    Posts
    104

    Accepting numbers in textboxes?

    Hi all. I would like to know if it's possible to accept numbers only in textboxes? In VC++ it's possible. But i can't seem to find in the properties that accept only numbers in the properties window.
    If it's not possible, then i would like to know how do i validate in the textboxes that the entered data are right format. If i were to check the textboxes by converting it to number using Int32.Parse method after a button has been clicked, it will throw an exception of invalid format. I would like to know how do i check the textboxes without throwinng an exception. Thanks

  2. #2
    Join Date
    May 1999
    Posts
    226
    It is rather odd they don't have an option to do this considering how common it is to want this. Atleast I haven't found one.

    Anyway, what I've done so far is add an event handler for key down. If the key isn't an allowable character then I'll change the text.

  3. #3
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    you can make a control having keypress event which only accepts digits and throws rest out

    methodName...(KeyPressEventArgs e)
    {
    if(Char.IsDigit(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == 8)
    {
    /// blah blah blah
    }
    }

    Paresh
    - Software Architect

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