News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Numeric Input from Edit Boxes

Started by Bieb, January 24, 2005, 06:54:48 PM

Previous topic - Next topic

Bieb

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?

00100b

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.

Jimg

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!

00100b

Jimg,

Would it save the EditWndProc some steps if one were to use SetWindowLong and the ES_NUMBER style flag for the Edit control?

raymond

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
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

hutch--

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.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php