Xojo 2023r4: WebTextField.SetFocus behavior

This is web app, I have web page with few web containers that have their sets of controls.
When user clicks on New toolbar button I want to enable a particular web text field on the first web container and set the focus (MyField.SetFocus).
I have also 2 timers on this web page.

I have noticed that after clicking on New toolbar button the field (MyField) gets enabled but the focus is not set (I don’t see cursor there). If I click on New one more time the cursor will appear inside MyField.

If I click on Cancel toolbar button to disable MyField and click on New again, the field gets enabled but does not get the focus. If I keep clicking New again (and again if want) the field MyField will get the focus.

Why is the SetFocus not working the very first time? Is it a bug or a feature that I don’t understand?
Can someone wise please explain this behavior to me?

Thanks.

@Ricardo_Cruz please help for this question …

Can you provide a sample project?

I have produced demo project. How do I hand it over to you?

I have created issue https://tracker.xojo.com/xojoinc/xojo/-/issues/75357 and attached zipped demo project with the description of what I expect to see.
Please review and let me know if my expectation is valid, thank you.

You can use dropbox, onedrive, wetransfer or other tool to share a project link.

I can’t get to that, maybe you created the Issue as ‘private’?

I did create the issue as private. I can’t find the way of changing it now via Edit button.

If you want to make it public, then you will need to put a note on your Issue. Then when Xojo review the case they can change it to Public.

No worries, I have created another issue with the attached demo zip file:
https://tracker.xojo.com/xojoinc/xojo/-/issues/75358

This seemed like a code execution timing issue so I added a 100ms WebTimer to the container and modified the code in your button:

MyField1.Enabled = False
Container1.MyField2.Enabled = True
Container1.Timer1.RunMode = WebTimer.RunModes.Single

The code in the WebTimer Run event:

MyField2.SetFocus

The result was the cursor shows in the WebTextField after you click the button.

My guess is a timing issue as not everything is executed lineal. The Web app may be creating the control and assigning the focus at the same time. I’m no expert on this so I don’t know if this a design limitation or a bug.

What you can do is:

  • use Shown event instead of Opening for: MyField1.SetFocus
  • use timer.CallLater on your buttons to avoid this timing problem somethin like:
timer.CallLater(0, WeakAddressOf MyField1.SetFocus)

instead of

MyField1.SetFocus

and

timer.CallLater(0, WeakAddressOf Container1.MyField2.SetFocus)

instead of

Container1.MyField2.SetFocus

to get this:
screencast2024-01-2216-15-48-ezgif.com-video-to-gif-converter

4 Likes

it is clear that a SetFocus call must be made in the shown event or later,
and not in the open event where the control we want to set the focus to may even not exist…

1 Like

Thanks for the prompt reponses, I appreciate it a lot. I have learnt two things:

  1. I need to delay SetFocus using timer to give xojo enough time to finish building control - I wasn’t aware of the non-linear method execution in the web app.
  2. I can use shared method of the timer object (ie. don’t need to create the instance of the timer) by invoking timer.CallLater method

Now that I know the above I need to figure out how to implement this into my actual web page and test. Thanks again.

In 2024r1, the web framework will do its best to wait for the control to be drawn. SetFocus will work even when calling this method from the Opening event, or in cases where it needs to be redrawn, to avoid having to deal with timers.

Thanks for reporting the issue.

2 Likes