Set the focus to a ListBox on Window Open

After one hour or so of research, I failed to find how to set the focus to a ListBox at window open time.

The window (main window) have two Controls: a ListBox and a TextField (read/write).

All in all, actually, the TextField always have the focus, I never been able to set the focus to the ListBox.

My question is:

How can I set the Focus to the ListBox at window open time ?

PS: I make a copy of the Selected Row (or the first selected Row if more than one Row is selected), into this TextField so I need to be able to copy the contents (whole or part) of the TextField contents (unfortunately setting the TextField as ReadOnly is not an option).

What happens if you use myListbox.SetFocus in the open event of the window? Is something stealing your focus? If yes, then you need to use the debugger to find the culprit.

It is strange. If the listbox is by itself, it get the focus. If any text control is added, even with a higher tabindex, it steals the focus.

Setfocus does not work, it is necessary to click on the listbox to get the focus.

A solution is to use the code discussed here https://forum.xojo.com/12285-move-mouse-cursor-and-click-by-code-for-mac/0

The trick is to have it click on the listbox.

[quote=241667:@Michel Bujardet]It is strange. If the listbox is by itself, it get the focus. If any text control is added, even with a higher tabindex, it steals the focus.

Setfocus does not work, it is necessary to click on the listbox to get the focus.

A solution is to use the code discussed here https://forum.xojo.com/12285-move-mouse-cursor-and-click-by-code-for-mac/0

The trick is to have it click on the listbox.[/quote]
If this is the case, please file a bug report.

OK. I was able to put this in evidence.

42216 - ListBox TabIndex or Setfocus does not work when a TextField is on the window
Status: Needs Review Rank: Not Ranked Product: Xojo Category: N/A

Michel Bujardet Today at 6:49 PM
OS: OS X 10.11.4

Xojo: Xojo 2015r4

Steps: See the attached project.

On the windows, there are a listbox, Tabindex 0, a button, TabIndex1, and a TextField, TabIndex2.

In the window Open event
ListBox1.SetFocus

When run, focus is on the Textfield.

Normally, it should be on the ListBox.

There are IMHO two bugs :
1 - Since ListBox1 has TabIndex = 0, it should have the focus
2 - ListBox1.SetFocus in Open is not honored

Setting focus in the button works fine

Expected Result:
The ListBox should have focus upon the window open

Actual Result:
TextField steals the focus

Workarounds:
Use a timer to set focus

<https://xojo.com/issue/42216>

Emile :
Drag a 100ms single timer on the window, with in its action event, something like :

ListBox1.Setfocus

On OS X you have an option in the system preferences, which is called “Full Keyboard Access”. It is under –> Keyboard –> Shortcuts. The standard is to tab only to “Text boxes and lists” (lists doesn’t mean the Xojo Listbox). Set it to “All controls” and it will work.

I see the focus on a Listbox without full keyboard access.

Hi Eli:

if I understand correctly:

do that and you will never be able to revert to the standard mode. Issue a ctrl-PoerKey and watch the focuns around the CheckBow (about close or not the windows)…

BTW: if you find a way to revert that behavior after a reboot: please tell me how to do that.

As an example, EyeTV change this behavior by itself each time it opens aTV window (when an external monitor is connected).

@Michel:

For once (my bad luck) this is a true bug.

For what is worth (and if I forgot to tell so):
When the TextField ReadOnly property is set, the ListBox get the focus.

At last: I will use the 100ms timer workaround.

PS: this is not a matter of the Show the surrounding Focus ring. This, as Michel stated, works fine.

For this one, I am unsure: sometimes I cannot get the focus to the ListBox unless I click on it (no Tab, arrow key, no copy / paste, nothing): when the ListBox is empty I think. But if the TextField is in Read / Write mode, the tab key allows to get the focus to the next (previous when Shift-Tab) Control.

Thank you all.

As of today, Michel bug report status is still Needs Review.

ps: yes, I decided yesterday to get another eye on that and after adding breakpoints, Self.Focus = ListBox1 (in Open, Activate, …) and many other things, Xojo 2016r1.1 still displays the bug.
If the breakpoint is just after Self.Focus = ListBox1, we can see ListBox1 have the Focus, but somewhere later ListBox1 lost the Focus. *

Nota: a search in Feedback using ListBox leads to 517 report entries (some of them were closed, not reproducible, duplicate, and so on); there is no 517 pending bug reports.

A brand new ListBox may be in the works ?

  • In a project, the main window have a Listbox and a TextEdit and 99% of the time, when I Paste data, it goes into the TextEdit: weird !

Timer Oh Timer, here I come…

The world would be only half as nice without timers. >:D

Event programming without timers would be like a slice of bread without butter.

Don’t neglect analog. The 555 timer is an extremely versatile integrated circuit.

There’s a lot of truth to the statement “Time is merely an invention to prevent everything from happening all at once.” :slight_smile:

My doctor said: “No butter Mr. Schwarz ! (nor sugar !)” :wink: