Set the focus (by code) to the Listbox

  1. 4 months ago

    Emile S

    Mar 11 Europe (France, Strasbourg)

    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 :( [I also tried KeyUp…]

    Both Controls have TabStop ON.

    Any idea is welcome.

  2. Michel B

    Mar 11 Pre-Release Testers RubberViews.com

    Have you set UseFocusRing to True ?

  3. Anthony C

    Mar 11 Pre-Release Testers, Xojo Pro GraffitiSuite Developer
    Edited 4 months ago

    Is the AcceptTabs property of your TextField set to True?

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

  4. Michel B

    Mar 11 Pre-Release Testers RubberViews.com

    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.

  5. Anthony C

    Mar 11 Pre-Release Testers, Xojo Pro GraffitiSuite Developer

    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.

  6. Anthony C

    Mar 11 Pre-Release Testers, Xojo Pro GraffitiSuite Developer

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

  7. Emile S

    Mar 11 Europe (France, Strasbourg)

    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 ?

  8. Michel B

    Mar 11 Pre-Release Testers RubberViews.com

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

  9. Emile S

    Mar 12 Europe (France, Strasbourg)
    Edited 4 months ago

    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.

  10. Emile S

    Mar 12 Europe (France, Strasbourg)

    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.

  11. Emile S

    Mar 12 Europe (France, Strasbourg)
    Edited 4 months ago
    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. ;-)

    Edit: add the Tab key press.

  12. Michel B

    Mar 12 Pre-Release Testers RubberViews.com

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

  13. Emile S

    Mar 12 Europe (France, Strasbourg)

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

  14. Michel B

    Mar 12 Pre-Release Testers RubberViews.com
    Edited 4 months ago

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

    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://docs.xojo.com/index.php/ListBox.Selected

or Sign Up to reply!