Question

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?

Was it helpful?

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.

OTHER TIPS

The documentation for ES_NUMBER (which is what I presume you're using) says:

Allows only digits to be entered into the edit control. Note that, even with this set, it is still possible to paste non-digits into the edit control.

To prevent pasting non-digits, you'll need to scan through the data in the clipboard and prevent pasting if it contains a non-digit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top