Thread: Restrict Textbox Input in C++

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    76

    Restrict Textbox Input in C++

    I'm writing a simple calculation program using C++. I'd like to restrict my textboxes to only except valid hex numbers as input. Is there an easy way to do this? I will post code if necessary but my code is all under the calculation button and I think this needs to be put behind the textbox itself. The default VS 2008 code is all I have there now.

  2. #2
    Registered User
    Join Date
    Jan 2011
    Posts
    87
    are you using char or int for the input?
    int wont accept letters

    once you are using char just use an if statement or a switch like this:
    Code:
    char hexnumber[6]
    for(int a = 0; a < 6; a++;
    {
          cin << hexnumber[a];
          switch hexnumber[a]
          {
                case '1':
                case '2':
                 ...... and do on,
                            then: 
                case 'F':
                   break;
                  default:
                cout << "that is invalid input try again";
                break;
           }
    or test the whole input after you cin it,

    i have no idea on how to do this in visual but it should be pretty easy to convert

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by dolfaniss View Post
    I'm writing a simple calculation program using C++. I'd like to restrict my textboxes to only except valid hex numbers as input. Is there an easy way to do this? I will post code if necessary but my code is all under the calculation button and I think this needs to be put behind the textbox itself. The default VS 2008 code is all I have there now.
    What UI library are you using? QT, WxWidgits, Win32, MFC, etc?
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    76
    Win32 library.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I believe you can do this by catching EN_UPDATE via WM_COMMAND, then modifying the edit control's text if it contains unwanted characters.

    If this doesn't work, then I think you need to subclass the control and catch the keypress events.
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  3. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 01:22 AM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  5. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM