Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How can I make the user of my program enter only one comma - Delphi7

+1 vote
asked Apr 11, 2023 by I A (330 points)

Sorry for the inconvenience, I have a question if possible: For example, suppose we created a program to calculate the average of a subject and entered the test1 point and the test2 point and then the average is displayed, so we need two edits to enter the test1 and test2 and one edit to show the average, and as we know that the test 1or test2 point, for example, if it is 15.5 It means that the point, if it is decimal, will consist of a comma, meaning it cannot be a decimal number that contains two commas, for example 15.15.1, as this is not a decimal number, so my question is: How can I make the user of my program enter only one comma, that is, how do I prevent him from entering several commas In order for the error messages not to appear, I apologize if my explanation was not clear, just what is the code that I write to make users enter only one comma, and where do I write it, for example in the edit itself or in the properties onchange or onkeypress or where exactly??

1 Answer

0 votes
answered Apr 13, 2023 by Peter Minarik (87,810 points)
I do not know Delphi.

In similar scenarios, you'd indeed subscribe to some event, such as OnKeyPress or OnChange (depending on the language, what these are called, and what they exactly do) that is called right after the user has pressed a key or the input is registered, but not displayed on the UI yet. Then you can make a decision on whether this new content should indeed appear.

To be more precise, assuming OnKeyPress is called right after a key was hit, but not displayed to the UI just yet, you should check the content of your edit box and if it already has a comma, then you do not allow this extra comma (remove it from the end of the edit box's string?).

This should give you the idea.

Good luck!
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.