Hi there,
Sorry for my newbie question. Is there an easy way when opening a window to make sure no fields have focus by default.

You’ve searched documentation.xojo.com ?
Search for “Clearfocus”

Well, since ClearFocus isn’t a method of the Window class but a global one, one must know its name to look it up in the LR…

Try searching for “Focus”
Read the list and keep going until you’ve read every entry or found one that looks right

That’s the 31 first entries returned when searching for Focus in the LR:

Changes for Real Studio 2012 R2
Changes 2014r1
Changes 2013r4.1

When I started programming in RB years ago I immediately stopped using the LR-Search but for the most simple queries like Listbox or TextField. For anything else you almost forced to google. Type in site:documentation.xojo.com Focus und you get this answer:

Window.Focus - Xojo Documentation
RectControl.SetFocus - Xojo Documentation
ClearFocus - Xojo Documentation

Thanks for the clues. I have tried clearfocus. But no luck… I have added it to the window open event, as well as the actual textfield open event that has the focus.

The Open event is called immediately before the control / window is shown. Focus is then automatically set to the first control in the TabOrder.

Why would you want to clear focus when the window opens? This contradicts all user expectations.

If only a textfield is on the window and it has tab index zero, it keeps the focus when you clearfocus. The best way I found to clear the apparent focus for the user is to have another TextField off window (at left = -200 or something) with Tab index zero.

I guess I am not being clear. When the window opens it has a number of controls. I don’t want any of these controls to be highlighted awaiting keyboard input. I want the user to actively choose what control they want to interact with first. At the moment it seems that the control with the lowest Tabindex is getting focus.
So, my question is how do I ensure that all controls do not have focus by default when the window loads.

Thanks @Michel Bujardet - I did consider that approach as well. But it seems very hacky so I am hoping there is a better way.

To tell you the truth I even filed a bug report about the nevr going away focus without much success. Being pragmatic, a hack is often preferable to a never coming fix…

Yes. unfortuantely it seems the only way for now.

Have you tried Window.SetFocus?

Start a short-period timer in the Open event. In the timer, call ClearFocus. Give it enough time to process the Activate event.

Thank you! After fiddling with this for long time I was finally pointed in the right direction.
The documentation on window.keydown could mention something about clearfocus and that it needs to be called with a delay. The logic was not clear until the problem was solved (now is, kind of). I still do not understand why the focus is automatically set to one of the controls and not kept at the window itself until I want it to do otherwise.

Well, in my program I needed to sense if a key had been pressed, to pause any action in the window. When the window had lost focus by being in the background, again something in the window had taken over the focus (which is strange to me) and the keydown didn’t fire anymore when the window was on top. I started the timer in the activate event instead and now it works as it should in this program.

I figured this could be useful for someone else so…

I’m having similar ‘focus’ issues, which are related to the tab index. I thought to disable a control from receiving focus, in the IDE you simply set the Focus Control, TabIndex to -1.

What’s the difference between -1 or Zero? I’m finding this very confusing.

Also the fact that at least 2 controls (or more?) can have the same TabIndex number in the same window . . . very confusing indeed and likely the source of my frustration. Although I have since realised that tab index is particular to the control surrounding it - not very intuitive in my view.

When you combine that with mouse click, then you have stepped onto “The Carousel that never ends”

@Adam Martin
Sometimes you have to do “dirty fixes”. Anyway, that is the basis of my programming style, and I’m sticking to it (just don’t tell anyone) :slight_smile:

TabIndex is zero based http://documentation.xojo.com/index.php/RectControl.TabIndex