Controlling entered Edit Control Text

This is a discussion on Controlling entered Edit Control Text within the Windows Programming forums, part of the Platform Specific Boards category; What would I need to do to be able to limit the range of characters the user could enter. In ...

  1. #1
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256

    Controlling entered Edit Control Text

    What would I need to do to be able to limit the range of characters the user could enter. In my currect program, their entry is supposed to be in binary, so how would I prevent them from entering anything other than a 1 or 0? Note that I know I could partially solve this with the ES_NUMBER Style.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    In MFC I subclass the edit.

    Derive a class based on CEdit
    Create the edit using your class, all the msgs you don't handle will go to the base class and be handled (as per normal).

    Handle the OnChar() messages for that edit.

    If the text entered is not correct then just return (so the edits text is not updated, the user pushes the key and nothing happens).
    (nChar != 0x30//VK_0 && nChar != 0x31//VK_1)

    If it is correct call the default handler
    ( CEdit::OnChar() )


    In WIN32 you would handle the WM_CHAR for the parent (dlg or window), ensure that the edit has focus (is getting the input), and then test.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    You can also subclass in win32 by calling SetWindowLong with the GWL_WNDPROC paramater.

  4. #4
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    WM_CHAR. Alright, I'll research that. I tried processing on the EN_CHANGE notification, but I ended up with some bugs.

    And, I've seen the SetWindowLong function on MSDN, but I have yet to use it. Perhaps this would be a good opportunity to teach myself. Thanks, a lot.

  5. #5
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    couldnt you just use getche()?
    Code:
    char num
    
    num = getche();
    if (num == '1' || num == '0')
          //whatever you want done
    Keyboard Not Found! Press any key to continue. . .

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 01:59 PM
  2. How can I send a text string to an edit control
    By marc74 in forum Windows Programming
    Replies: 5
    Last Post: 01-06-2005, 09:14 PM
  3. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 06:35 PM
  4. adding text to an edit control
    By steve_i83 in forum Windows Programming
    Replies: 2
    Last Post: 10-26-2001, 06:40 AM
  5. Controlling an edit control
    By master5001 in forum Windows Programming
    Replies: 2
    Last Post: 10-16-2001, 03:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21