Help with TextBox.TextChanged event

Hello there,

I am doing the following validations (and automatic corrections) on some text boxes
-max length 3char
-no space char
-to uppercase.
I attach the code.

Note that inside the event handler, if the validation fails then I correct the text so there is reentry in the code, which will not modify the text again because of previous correction (there will be no more reentry).

The problem is that the handler is not invoked every two changes. For example
Scenario 1)
-If the user presses SPACE, then handler is invoked and the space is removed).
-If the user presses space again (no matter how long it takes to do so), then the handler is not invoked, so the uncorrected space is displayed.
Scenario 2)
-The user enters three letters, for example, ABC.
-The user enters a fourth letter, then the handler will correct it and the user will still see ABC.
-The user enters another letter again, but this time the handler is not invoked. So the fourth letter is displayed, for example ABCd.

Please can you help me see what is wrong.

Thanks in advance,
Osmany

Double check the blank text blocks to insure they have length> 0 .

The blocks Editor trims them.

If that doesn't help, upload a small test aia export.

Hi @ABG,

There it goes,
PCopy.aia (3.5 KB)
Thanks,
Osmany

Yes the error is this :

Yes @HarshVardhanSolanki , also try SPACE.

What is really strange is that the correction to uppercase does not produce this situation, the handler is always invoked.

There is error or a bug in the builder

I use this in the splited value you can really see the 3 letters,

but look at my mobile screen there are 4 letters which are VAAj

Dear @ABG,
Kindly please, let me know if you find out anything.
Thanks,
Osmany

I hope you understand that Power Users could be sleeping or are doing other things. Power Users spend their free time helping in the community. So there is no need to spam the topic, just be patient.

1 Like

Good morning from GMT-5

I will have access to my PC in a few hours after breakfast.

1 Like

I added a trigger counter to the Text Changed event, to see how many times it fires, in the hope of seeing it skip a fire opportunity.
Sample run

To my surprise, the trigger happens when I enter a letter AND when the event updates the .Text contents.

I am guessing behind the scenes, this event is trying to avoid getting into an infinite loop of being triggered by its own actions.

P.S. I double checked your blank and empty text blocks, and they are correct.

2 Likes

I tried several variants of the code in this event, and the behavior persists.

I recommend switching to a fast single purpose Clock Timer instead.


PCopy (1).aia (3.6 KB)

From what I see, I can't recommend using this event.

1 Like

Use Taifun TextBox Utils, may be it can help you for not getting skips.

2 Likes

You can find the extension here

Let us know, if you get the same issue there

Taifun

1 Like

Hi @ABG,
Sorry it took me a while to give feedback, I was running a lot of tests.
TextBox does not fire the event after correcting, for instance, 'ABCd' to 'ABC' and then user type letter 'd' again. This text change confuses it by returning to a previous value, i guess.

I have decided to move to TaifunTextBox (as suggested by @HarshVardhanSolanki). I just had to make sure in the event handler that it doesn't fall into recursion until stack overflow. I used a reentry counter, with a value equal to one (first entry) as a condition to be able to modify the text inside the handler.

counter = 0
when TaifunTextbox1.AfterTextChanged
     counter = counter + 1
     if counter equal 1
           if validation fails
                 set tb.Text to newValue
     counter = counter - 1

@Taifun, The pseudo code implementation above works perfectly, the event handler is always invoked.

I'm left with the feeling of not knowing what's going on with the TextBox. Exactly the same code works using TainfunTextbox.

Thank you all for your time,
Osmany

2 Likes

For the sake of completeness, here's an extension-free implementation based on a special purpose clock timer.


PCopy.aia (3.4 KB)
Sample run

3 Likes

Great solution! @ABG :clap: :clap: :clap:

I knew I would only solve this by using the Timer... but I wasn't sure how to implement it... and I was going a completely wrong way, disabling the component, using the timer and activating the component on the way back. .

The initial programming of this topic is a masterpiece to exemplify how block processing works and how the triggers of Events work. :pray:

Lito

@>-->---

There is no need for you to keep asking people to mark solutions. It will get done. Please stop doing this, it just clutters up the community with unnecessary posts.

3 Likes

Hello everyone,

For the sake of knowledge (not such a practical approach), here are my personal observations.

  • TextBox.TextChanged event can be re-entered when TextBox.Text property (the trigger) is changed (pulled) within its code. Care must be taken to avoid throwing a stack overflow.

  • I'm still unsure if TextBox has any issues. Exactly the same code does its job well within TaifunTextbox.AfterTextChanged event handler. The unanswered question is why does the input sequence 'abcde' triggers the event on the last text change (typing 'e' after auto removing the fourth letter), but 'abcdd' does not.

I kindly thank @ABG again for rolling up his sleeves and work on an alternative solution.

Best regards,
Osmany

1 Like

A good observation.

Here are two hypotheses:

  • Some software layer is trying to de-bounce the simulated keyboard input (not likely)
  • There is a race condition exposed by the faster keyboard input speed of holding down a letter versus switching letters. There are essays on the AI2 thread model at FAQ Section: Waiting and Timing but I am not prepared for such a deep dive.

@ABG,

Type sequence kkkm, wait as long as you want, and press m again. You will see that it does not trigger the event.

It is as if a condition were met to not invoke the event handler. I suppose that the value in the box is exactly what caused the previous trigger.

We are guessing here :smiley:
Greetings,
Osmany