The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Bieb on January 24, 2005, 06:54:48 PM

Title: Numeric Input from Edit Boxes
Post by: Bieb on January 24, 2005, 06:54:48 PM
What's the easiest way to accept input in the form of numbers typed  into edit boxes, and then translate the ASCII data into the appropriate numbers?
Title: Re: Numeric Input from Edit Boxes
Post by: 00100b on January 24, 2005, 07:13:14 PM
One method would be to use the GetWindowText Win32 API to populate a buffer and then iterate through the buffer, subtracting 48 (decimal) from each byte up to the null-terminator.  Based on their position, multiply by 10^position and then accumulate the total.
Title: Re: Numeric Input from Edit Boxes
Post by: Jimg on January 24, 2005, 07:36:05 PM
Another way is to subclass the edit controls like this:

Assuming you have an edit box with with id 1001

In Main message loop:

   .elseif uMsg== WM_INITDIALOG
        invoke GetDlgItem,hWnd,1001
        mov hEBox,eax         ; save handle to control
        ; subclass the edit control so we can see what keys are pressed and act accordingly
        ; wParam=handle of control to receive focus
        ; lparam=0 from above call to DialogBoxParam
        invoke SetWindowLong,hEBox,GWL_WNDPROC,offset EditWndProc
        mov OldWndProc,eax
        xor eax,eax   ; False - don't set focus to first control
        ret

EditWndProc PROC uses edi esi ebx hEdit:DWord,uMsg:DWord,wParam:DWord,lParam:DWord
    ;traps messages to edit box to check keystrokes
    .if uMsg==WM_CHAR
        invoke GetFocus
        mov edx,eax
        mov eax,wParam
        .if edx==hEBox ; check for valid characters 0-9 or minus or backspace
            .if (al>="0" && al<="9") || al=="-" || al==VK_BACK
                jmp EditWndContinue     ; good, just let windows process
            .endif
        .endif
    .else
        jmp EditWndContinue
    .endif
    xor eax,eax ;set that we have processed the event
    ret
EditWndContinue:
    inv CallWindowProc,OldWndProc,hEdit,uMsg,wParam,lParam
    ret
EditWndProc Endp
                 
Then when you want to use the value:

    invoke GetDlgItemInt,hWnd,1001,0,TRUE    ; get value



You will also have to add more code to guard against the user putting the minus sign in an inappropriate place!
Title: Re: Numeric Input from Edit Boxes
Post by: 00100b on January 24, 2005, 07:40:29 PM
Jimg,

Would it save the EditWndProc some steps if one were to use SetWindowLong and the ES_NUMBER style flag for the Edit control?
Title: Re: Numeric Input from Edit Boxes
Post by: raymond on January 24, 2005, 08:23:33 PM
The ES_NUMBER style seems to prevent the use of the "-" sign. If the use of only positive numbers is acceptable, the easiest way is then to use the GetDlgItemInt without checking for errors if you limit the number of characters to 9 in the edit control.

You can still use the GetDlgItemInt function with any edit control style but you then have to check for possible input errors. Study the description of the function.

Raymond
Title: Re: Numeric Input from Edit Boxes
Post by: hutch-- on January 27, 2005, 06:01:55 AM
Its always better to subclass the control and filter the input characters yourself as you have far more control and its relatively easy to write.