Public Group

# Edit Textbox, Numbers only?

This topic is 1745 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi,

I was wondering if anyone could help me with some basic WinAPI, I have succeded in creating a textbox where I can change and get the value from. But how should I procced to make it accept nummers and ' . ' alternative ' , ' and ofcourse ' - ' for negative value. I create the textbox with theese parameters.

CreateWindowEx
(
WS_EX_CLIENTEDGE,
"Edit",
content.c_str(),
WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL,
50,
0,
100,
50,
phWnd,
NULL,
NULL

); 

I tried to use ES_NUMBER, but it does not seems to support negative value or periods. So I tried to catch change's inside the textbox with EN_CHANGE. I manually remove's undesired change's. It works great except that its not friendly to input change's to the textbox seeing that I have to update the content with SetWindowText() or SendMessage(WM_SETTEXT), but each of these two will reset the marker of the textbox. Technially it works but its not so work friendly or efficient to move my mouse to reset my marker incase I want to change more than one nummer.

So I was wondering if any of you guys had any tip to resolve this "problem".

##### Share on other sites

Yep, ES_NUMBER sucks (because of no negatives or decimal points).

You either have to roll your own or search tinternet...

You can set the caret position by sending a message EM_SETSEL I believe (pass the start position as -1 and the end position where you want the caret).

The other option is to allow anything to be typed and validate the contents and clear them/revert to last good value if they are invalid when the control loses focus.

EDIT: You can also trap WM_CHAR messages when the control has focus and make an annoying beep and eat the character if the input isn't numeric, -, . or ,

You need other rules as well though i.e. - sign only as first character (unless you allow scientific notation e.g. -1.2e-5) and no more than 1 decimal point. You should probably allow + as well if you accept -, for completeness.

EDIT2: You probably want to subclass the edit control once you are happy with it so it is reusable in other dialogs/windows/projects.

##### Share on other sites

Oh, I didn't knew about SETSEL, I belive that this one will be the soloution of my problems!

Thanks for the quick respond!

##### Share on other sites

If -1 for the start position doesn't work you can try setting the start and end selection positions to be the same. I've not tried either method, presumably one of those should work.

##### Share on other sites

If -1 for the start position doesn't work you can try setting the start and end selection positions to be the same. I've not tried either method, presumably one of those should work.

-1 didnt work, so I just putted the start and end value as the same when I call EM_GETSEL

• 9
• 16
• 9
• 13
• 41