Set the focus (by code) to the Listbox

I have a Listbox and a TextField (as selected Row contents displayer) / mirror on my main window, and sometimes,none have the focus.

Event a Tab press does nothing.

I just have an idea; in the KeyDown Event, I put the following code:

// Try to pass the focus to the Listbox… Self.Focus = LB // LB is the name of my Listbox

But no effect (not the one I was awaiting). not even a beep :frowning: [I also tried KeyUp…]

Both Controls have TabStop ON.

Any idea is welcome.

Have you set UseFocusRing to True ?

Is the AcceptTabs property of your TextField set to True?

EDIT: just realized you’re having trouble setting it programmatically. Sorry about that.

There is not AcceptTab property on a ListBox. But I did verify it displays a focus ring wen setFocus and UseFocusRing is true.

Which does not mean it will look like it does when a row is clicked.

Nope, as I said, property of the text field. When True, pressing tab on the text field attempts to insert the tab character rather than switching focus, but that’s irrelevant if he’s trying to switch focus programmatically.

There’s also the SetFicus method of RectControl, iirc, which may work for you. MyControl.SetFocus()

Thanks for your answers.

UseFocusRing is set to False. [Like TextField; I do not like to see these Focus Rings… Not all platforms / COntrols can have a FocusRing and I do not want to fake it. A choice.
AFAIK, the problem is not to set the Focus, but to select a Row / be able to Edit a cell in a Selected Row.
Pressing Tab(s) does nothing (sometimes). Ennoying.

Listbox.KeyDown is trapped before WIndow1.KeyDown…

In fact, WIndow1.KeyDown is not fired (seems to be my fault.

I set the Focus lines in Listbox’s KeyDown, but still no selected Row, sometimes.

Of course, a click in the Listbox or/and TextField set the Focus and it keeps there until… (I do not know).

BTW: Did I told you that I am a keyboard man ?

Maybe I have to trash that idea ? Your opinion ?

EditCell is what you need, or Listbox.Selected(), instead of SetFocus.

Thank you Michel.

But, how, with a keyboard key press I fall into the EditCell Event ?

Nota: I want to be able to select a Row when the Listbox (and the TextField) does not have the Focus.

Back to usual testing:

in a brand new project (created for the testing) with a Listbox and a TextField, the problem exists too.

But, issuing a Tab set the focus to one of the two Controls.

I check that…

Seems to be my fault; something non obvious happens in my Listbox.KeyDown Event.

something non obvious: I do not trap the Chr(9) key there, but other keys. I have to rad that Event carefully.

something non obvious

Faultive code:

If LB.SelCount < 1 Then Return True End If

Correct code:

 If LB.SelCount < 1 Then Exit

With the correct code, Tab (to get the Focus) and then a key press on one of the arrow keys select one Row.

Thank you all ± 7Bits viewers. :wink:

Edit: add the Tab key press.

EditCell is not an event, it is a method :
http://documentation.xojo.com/index.php/ListBox.EditCell

That is what I wrote… (more or less; not the answer).

Emile, I am afraid your are way too confused to get anywhere.

To select a row, use ListBox.Selected() (see above, I already posted that)

http://documentation.xojo.com/index.php/ListBox.Selected