Thread: Edit controls and numbers

  1. #1
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120

    Angry Edit controls and numbers

    Howdee, I want the user to only be able to enter numbers into an Edit control

    This would be simple enuff but I want the possibility of negative numbers and decimal points.

    Is there anyway of formatting the imput into an Edit Control, and once I've done that, is there a way of converting a string such as "-56.78" into a floating point number (without having to code my own function to do it! )

    Thanks,

    Dom

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Are you using MFC or straight Win32?

  3. #3
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    str8 Win32 API
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I know you can restrict the input to positive integers, but I don't thinkyou can do it with floats.
    You could allow the user to enter whatever gibberish he wants, but when he pressed OK (or whatever) you run some code to see if the input is valid. If it's not, pop up a message telling so.

    Or you could write your own input edit control...
    (FOOTNOTE: Don't ask me how to do that)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    The ES_NUMBER style will not allow non-digits to be entered, however, I can't remember off the top of my head if it allows minus signs and decimal points. Try it.

    If it doesn't work, subclass the editbox and dump any unwanted messages. The program below is an example of subclassing I wrote for someone else, change it so that if the WM_CHAR is a character you want, pass the message on to the edit control, if not just dump it.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    Thanks guys, I wrote my own Edit Class in the end with a function to clear out any non valid text from a string - this code is run every time a key is entered, a little innefficient perhaps but it works! There are also (I eventually found) a couple of really nifty functions in stdio.h that convert strings to numbers (I managed to write my own but encountered some strange mathematical accuracy results like "87.99" -> 87.990000003 ???!).

    For future reference they are:

    Code:
    double atof(char *string) -- Convert string to floating point value. 
    int atoi(char *string) -- Convert string to an integer value 
    int atol(char *string) -- Convert string to a long integer value.
    Just bunged in a GetNumber method to my Edit class that uses those functions and bobs your uncle!
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Controlling edit control input
    By Gerread in forum Windows Programming
    Replies: 6
    Last Post: 05-03-2007, 08:56 PM
  2. Auto Tab Edit Controls
    By willc0de4food in forum Windows Programming
    Replies: 1
    Last Post: 09-07-2005, 01:59 PM
  3. edit control - testing input
    By Micko in forum Windows Programming
    Replies: 2
    Last Post: 08-04-2004, 01:23 AM
  4. floating point variables in edittext controls
    By dootickle in forum Windows Programming
    Replies: 3
    Last Post: 04-15-2004, 11:15 AM
  5. Why won't my derived controls work?!
    By Dual-Catfish in forum Windows Programming
    Replies: 5
    Last Post: 05-21-2002, 04:23 PM