Strange behavior of the program when synchronizing the values of the textbox and the slider

In the project I am trying to create a global variable, the value of which can be changed both by entering text and by using a slider.
Accordingly, the number in the text field and the position of the slider should be synchronized both in one direction and in the other. However, simply entering a number in the text field does not change the variable, so I had to add a button and initiate overwriting the variable on click.
At the same time, after saving the entered text, the variable takes the wrong value that was printed, and with an unclear pattern. How to solve this problem?
Moreover, it is desirable that the previously recorded value is saved when the program is opened later. What is the best way to add this feature?
In the project, the variable has the name "Period".
Change_variable.aia (45.7 KB)

The global initialize variable cannot be a TextBox.

Using this @Taifun extension you can control the change of the TextBox.

You can also use a Clock to control the change in the TextBox.

Pull in the TinyDB component from the Designer Storage drawer.

Save the global value in TinyDB under tag 'PERIOD' when it changes.
At Screen1.Initialize, load the global valuevalue from TinyDB tag 'PERIOD', with default value 0 or something appropriate like the Slider.Thumb or the Textbox.Text value. Update the display components from the loaded value too.

But I requested the text from this object. Can't it be written to a variable? Or is there a problem with the types of values? By the way, how does Ap Inventor convert variable types? There is only a number type conversion in blocks. How is text converted to a number, and vice versa?

AI2 uses Duck Typing. (If it looks like a duck, walks like a duck,... it is a duck).
It will attempt to convert between text and number types as needed, as long as the text is non-blank.

There are a bunch of test blocks like is_number to help guard against conversion fails.
Typeblock 'is ' to see them all.
Duck Typing does not extend to text representations of lists or dictionaries.

And then how is an empty string interpreted when entering data into a mathematical expression?

You will usually get an error if you do not set a default value e.g. "0"

But then how to ignore the input if the user left an empty field in the TextBox and pressed Enter?

Use the following pseudo code

If not isEmpty Textbox.Text
then do the calculation


The user can erase the digit and press Enter.

It seems to have done everything correctly, but the figure in the TextBox is not displayed, and it does not react to the movement of the slider. What is the error?
Change_variable2.aia (61.6 KB)

Maybe something like this:

Did as you suggested, added Slider extension, added commands. Really didn't figure out why they are? I started the emulator - the text is still not visible, but now when you press Enter, the slider always shifts to the right.
P.S. I inserted the "round" blocks later, but it didn't change anything.
P.P.S. the problem with the visibility of numbers in the TextBox was solved by setting the height of the box to automatic. Now I see that the movements of the slider are displayed in the TextBox, but changing the text does not synchronize the position of the slider.

Change_variable3.aia (72.5 KB)

Observing the work of my first option, I discovered a pattern - when the first two-digit number is set in the TextBox, the second digit turns into one. With a secondary set of the same number, it is stored correctly! When typing a three-digit number with a non-zero third digit, this digit is also reset to one. If the third digit is 0, then at the first set, the second digit decreases by a dozen. But in any case, when the same number is repeated, the set is saved correctly!

Added the Taifun Textbox extension function to its original version. When you change the text in the Screen 2 line and press Enter, nothing changes. They don't seem to be working properly either.
Change_variable (1).aia (63.4 KB)

Have you read Taifun's examples of using this extension?

1 Like

I added the TextBox content tracking initiation from Taifun Textbox to Screen2. Nothing has changed.
Change_variable (2).aia (63.1 KB)

Try this one:
Change_variable_4.aia (63.2 KB)

This is like a snake biting its own tail. The Textbox changes to the Slider and the Slider changes to the Textbox, a stack overflow occurs.
To avoid this, use the Slider extension.

Change_variable_3.aia (29.0 KB)

When I replace the text, clearing the previous one to an empty TextBox before entering a new number, the same error pops up. In addition, for some reason, the wrong number is displayed in the window, which I enter on the keyboard!
When replacing the control unit with a reaction when entering text in the TextBox, it is saved correctly, but it is not synchronized with the slider.