We turn on the computer and run our development tool - XE5. We create a new project with the same empty form. (I showed how to do this in a previous article)
We throw on the form of TLabel, TButton and TEdit, how to do this I also showed in a previous article. Arrange and change the Text somehow like this:
As you can see, the Edit property of the Text property can also be changed. What is written there and will be written in it when the program starts. This value can be deleted or changed as the program runs. And yes, do not forget to put anchors - anchors to the components.
Now, it's time to get acquainted with another beautiful property TEdit'a. Surely, it is inconvenient to click on Edit and see the keyboard for entering letters in the digital field. Therefore, we will immediately open the numeric keypad. So how, open the Edit properties again, look for KeyboardType. This is the type of keyboard that will open when you click on the field. Change to vktNumberPad.
I would also like the NumberOnly property, but it is present only in computer TEdit. So we will write a little later a small check on the validity of the entered.
Oh yes, I almost forgot. It is also necessary to put on the TTimer form, from the “System” tab.
How you position it does not matter. This component is working, but invisible to the human eye.
And, you also need to turn it off so that it does not work when you turn on the program. We will turn it on at the touch of a button. To turn it off, you need the property: “Enabled” c “true” wave to “false”.
Now, click at the bottom of the page on "Code" and start writing the feature.
We write about this code:
Wait a minute, explain what’s what.
function is a keyword. It’s like a “procedure” created by double-clicking.
isnumber is the name of the function.
(s: string) is like entering a variable to work with a function. Therefore, in brackets there cannot be a value of another type. Well, by the end of the lesson you’ll understand more.
: boolean is the exhaust. Those. that the function will return. boolean is a type. It, unlike String'a, Integer'a, real'a, etc. conditional. Well, it can only be equal to “true” or “false”. Exactly the same as some fields in properties.
Well, surely something else is not clear. Examples of code will further dispel your misunderstandings.
We will check whether the number is entered using the condition try .. except .. end; - This is the easiest to understand, but not the easiest way for the device. Then maybe I'll show you another way. Easier for the device, but harder for perception.
Result is a variable answer. How would the function return in response what happened in this variable. By the way, we have a boolean type.
So the essence of the method:
If String can't be converted to Integer, it means there are non-numbers characters in the variable. Actually, the try handler.. Except.. End; Error handler. I.e. if the line between try and except cannot be executed for any reason, then what is between except and end is triggered.
So, if you can convert from String to Integer, Result will remain true. Otherwise - false. If the function will return true, then yes, in the variable number. Otherwise, no. You'll find out.
Now, climb up, where var. We need to add a variable. For example, i is an integer type.
So now we're back on form.
Now I will help you figure it out.
if (if) isnumber (Edit1.Text) = true (the function returned “true”) then (then)
begin (the beginning of the actions to be performed if the function returned "true")
i: = StrToInt (Edit1.Text); (i: = (assign to variable i) StrToInt is a function that converts from String to Integer for one action, i.e. only to assignment. From String to Integer we convert just the same value from Edit to assign to the variable value. Which is of type Integer.)
Timer1.Enabled: = true; (We enable the Enabled timer. We simply assign “true.” This is done just like that.)
end (End of action if function returned true)
else (Otherwise. That is, if the function returned false or something else)
begin (Beginning of actions if the function returns false or something else)
showmessage ('Invalid value!'); (We display a message about the "incorrect value")
end; (end of action if the function returned false or something else)
Now, again we return to the form and click now on the timer twice. An event handler is generated that fires once every N ms. These milliseconds are indicated in the "Interval" property of the timer.
The default is 1000 ms, i.e. one second. All perfectly.
We write here this code:
Essentially the same design.
If i = 0, then write "Skidysh !!! And turn off the timer. Otherwise, we simply take the unit from i and assign the Text'y label the number of seconds left before the “explosion”.
You can connect the device (if not connected), and click F9.
P.S. A lot of things distracted me when writing an article. Perhaps I missed something, tell me - I’ll supplement it. If you have questions - ask, I will answer.