TextArea Control Not Able to Receive Focus

For Windows target, deving on Windows:

I’ve got a Startup_Window that gives the user the option to open documents in another window. When the user clicks, I open the Doc_Window, hide the Startup_Window, and in the Doc_Window’s “Activate” event, I have “txt_Document.SetFocus.” However, the control doesn’t receive focus! The entire Doc_Window remains without focus, even though I just Showed it. WTF? What do you think is going on?

What target is your app for? and which OS are you developing in?

Windows target, developing in Windows.

It works for me. Probably some other control is “stealing focus”.
The workaround is to set a timer with around 3000mS. On the timer’s Action event, set focus to the control you want and disable the timer.

Would you mind sending me your working project and let me see if I can glean a clue from it?

Are you hiding the Start_Window from itself or from the Doc_Window? If the hiding is done from the Start_Window, then likely the focus has returned to the Start_Window so it can hide itself. If it were me, I’d hide the Start_Window from the Open event in the Doc_Window and set the field focus from the Activate event handler. Perhaps what you’re seeing is a ramification of the sequence of the actions.

Use another event For example open event of the textarea to set the focus.
Try that, also set the focus in the Activate event of the window just to be sure.

Perhaps it would help.

Yes. Actually, the code sequence in Start_WIndow is

Self.Hide win_Main.Show

When win_Main is Shown, its Activate event fires and assigns focus to the TextArea, except the TextArea doesn’t get focus.

Sounds like a good idea. I’ll give it a try and let y’all know.

[quote=220671:@Derk Jochems]Use another event For example open event of the textarea to set the focus.
Try that, also set the focus in the Activate event of the window just to be sure.[/quote]

The window is already open prior to this, so the open event doesn’t get fired when I show it.

To reiterate, I’ve got two windows: win_Start and win_Main. Upon a click event in win_Start, I call

Self.Hide win_Main.Show

This does what it should. win_Start gets hidden and win_Main shows up.

In the Activate event of win_Main, I do

txt_Document.SetFocus

What’s happening at this point is that win_Main doesn’t have focus. If I click on the window titlebar, whatever control I give focus to (I have a few other controls on there and I tested this out), ends up having focus when that window becomes active. So the control is receiving the SetFocus command, it’s just that the window, win_Main, is not retaining focus for some reason! Ugh! Really?!

I guess I should step through the code and see where it’s failing…

Have you tried showing the main window first and then closing the start one after ?

If not, maybe the focus is being lost because there may be a very short time period between the 2 events where you have no windows open at all, so how then would the main window know to get the focus ?

Another option may be to set focus in the main window open event and then set the focus of the text area after that.

Focus can be a little squirrelly. A very short period timer works wonders. Alternately, you could use some declares to force the focus, but really, a timer is the way to go. Start the timer in Activate and call SetFocus from the timer.

To use the Activate event if the Window to set the focus to a specific TextField might not be good interface design. If the user has more TextFields then he expects the focus to be where it was when he left the window, not in the first field again.

So you should use the Windows Open event to set the focus. That way it gets set once, at the original open, as it should be.

I’m also not sure that the Activate event fires at the original open as you expect.

Well i was kinda thinking to use the window open event to switch focus to the window itself, then the control focus could be set when it is opened.

Ive never considered setting a control focus from a window open event in an attempt to make it do both. That doesnt sit right with me even if it did work, although im not an expert in such matters.

I know Xojo has a separate issue with opening & closing windows from within each other when using sheet style windows; thats when i first considered the sequences for opening, setting focus, closing etc.

I’ll happily take the knowledge from those who are more experienced though.

A tad of pragmatism goes a long way to insure apps that run reliably.

You could also use the open event of the control itself, but the Window one is better.

First all Open events of the Controls are called, THEN after all the Controls have done their thing the Open event of the Window. So that one can then deal with setting the focus to whatever control you want.

But the Activate event is definitely the wrong one to use, and is probably your problem. The sequence of events is possibly

Window activate <- you set focus
Controls open <- each grabbing focus for itself
Window open <- Window got focus?

So there you see where you go wrong and also where you should put your code.

As for “using the Window Open event to switch focus to the Window itself”: Why? That seems a very convoluted way and is completely unnecessary. When the window opens then it HAS the focus, so what is the point of giving it the focus again?

Btw have you checked the tab order? Doesn’t the control with the lowest tab value get the focus when a window opens?

Yes, I’ve tried that. And I tried it again. No success. :-/

The main window open event gets fired when the app starts, and never again. It only ever gets activated after that. But, even setting focus to the textarea doesn’t resolve this issue.

That’s a good point, and one I’ll have to consider. At the moment, the main window comes open only when prompted from the start window. But, the user could minimize it and then get back to it and be annoyed by that behavior. I personally hate that behavior myself, so I can’t subject my users to that! :slight_smile:

The problem is, upon closing the start window and activating the main one, the main one doesn’t retain focus. :cry:

Thanks, Tim. I’ll add a timer and see how it goes…

It would be better if you posted the code in the open and activate events, and every place that uses SetFocus. My feeling is that you have a SetFocus in some window open or activate event which interferes.