Jump to content
  • Advertisement
Sign in to follow this  
Conny14156

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.

If you intended to correct an error in the post then please contact us.

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,
		(HMENU)Transform_Text_Box_Top_Left,
		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 this post


Link to post
Share on other sites
Advertisement

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.

Edited by Paradigm Shifter

Share this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!