Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
INTRODUCTION AND RELEVANT INFORMATION:

I have an edit control that needs to accept only signed decimal numbers ( something like -12.35 ).

I have decided to implement this via subclassing.

The WM_CHAR handler seems to works well, and I need to handle several other messages to completely protect user from entering invalid text. One such message is WM_PASTE.

So far I was able to properly get the text from the clipboard and to discard or pass the message depending if the copied string is indeed decimal number.

Edit control has limited input to 12 characters. This is done via EM_SETLIMITTEXT message.

I use pure Winapi and C++. No libraries like boost etc are allowed.

PROBLEM:

So far I test the case where edit control is empty and I discard pasting if the content is invalid. However, user can select a portion of the text in the edit control and then perform pasting. Something like this ( red characters represent selection ):

Text in edit control: 12345678.9

Clipboard text : -1A

The resulting string, if I would allow pasting, would be 123-1A78.9 which is invalid format.

This is the part I need help with:

My parsing properly recognizes valid format but I do not know how to properly merge two strings into one so I could test it.

QUESTIONS:

1. How can I properly form the resulting string from pasting so I can test its validity ?

2. Is there a function that can do the validity test for me ( it should be locale aware! ) ?

Note: I do not mind parsing the text myself but why "reinventing the wheel" if there is no need for it?

MY EFFORTS TO SOLVE THIS:

Browsing through Internet I found many std:: functions that concatenate/cut off/ etc string but none of them seem to satisfy my needs for forming the resulting string. The closest thing I found was string:: insert(...) but I do not know how to use it.

As for the second question, I found std:: strtod by browsing through C++ reference and it seems that it can be used to check the validity of the string. And it seems that it is locale aware as well, but I am not sure if it is what I seek.

To keep this post as brief as possible I provide the link to this question of mine[^] that has relevant code snippets.

Thank you for your time and help.

If further info is required I will update the post.

Best regards.
Posted
Comments
chaau 12-Mar-14 1:17am    
I think these type of validations need to be done in ON_EN_CHANGE handler. This will cover both the direct user input and Paste from clipboard
AlwaysLearningNewStuff 12-Mar-14 14:20pm    
Is there a danger of endless recursion in this case? If there is, what measures could I take to prevent it from happening ?

Thank you for your suggestions.

Best regards.

1 solution

The answer to your first question is: Just build a new string by concattenating
a) the part before the selection
b) the clipboard text
c) the part after the selection.

So it is as simple as two concattenations.

The answer to the second question is: Yes, you could use strtod for the validity check. But consider that strtod accepts a wide range of formats, e.g. "12.4e-13". Possibly you don't want to allow the user to enter exponents. In that case you will have to write your own validation function, which is not too hard.
 
Share this answer
 
Comments
AlwaysLearningNewStuff 12-Mar-14 13:23pm    
Thank you for your help. I guess I will have to do everything on my own since I do not allow exponents. 5ed. Best regards.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900