Search code examples
cwinapidialognotepadeditcontrol

How to disable pasting words into a number-only edit control?


I am writing in C WinAPI a 'Go To Line' dialog of the notepad. I created a number only edit control. But I can still paste words into the edit control! The dialog in the windows notepad does stop this kind of pasting. So how can I do the same thing as it in the notepad?


Solution

  • Subclass the edit control, and when WM_PASTE is received:

    OpenClipboard
    GetClipboardData
    GlobalLock
    

    Now use the returned pointer from GlobalLock to check for non numeric characters. If a non number is found, inform user then:

    GlobalUnlock
    CloseClipboard
    

    and return 0 from the callback to prevent pasting the data into the edit control.

    If it is all numbers, then GlobalUnlock and CLoseClipboard and pass message on with CallWindowProc to allow paste.