Unable to set the focus on a Canvas

The code below sets the focus only if the MsgBox line is not commented:

[code]Function MouseDown(X As Integer, Y As Integer) As Boolean
#PRAGMA Unused X
#PRAGMA Unused Y

Dim Focus_Name As String

// On Click, set the focus
Self.Focus = Me

// No visual change
Focus_Name = Self.Focus.Name

// // Of course, a MsgBox is displayed (with the Canvas name: Canvas1)
// // And the focus is set once the MsgBox is closed…
MsgBox Self.Focus.Name

// Ask the Canvas to Refresh
Me.Refresh // Does not works: Invalidate(True)
End Function[/code]

Create a new project, add a canvas, copy/paste the code above and run in the IDE.
Comment the MsgBox line, run and cry.

I did not cry, but I lose another testing project in checking that (trash the Xojo cache contents, trash the generated applications and at some time, a folder near the applications goes to the Trash and… is lost.

I can confirm that. Just tried it on OS X 10.11.3. My setup is:

  1. TextField
  2. Canvas with AcceptFocus = True and UseFocusRing = True set both in the IDE, Self.Focus = Me in the MouseDown event, and BREAK in the KeyDown event
  3. Button with code MsgBox Self.Focus.Name

And this is what happens:

  • The window opens with the text field having focus.
  • Pressing the button shows the message box with TextField1.
  • One mouse click on the canvas (if you set a break point in this event it will be hit, so Self.Focus = Me really runs)
  • One keystroke, which doesn’t trigger the KeyDown event of the canvas but shows the character in the text field
  • Pressing the button shows the message box with Canvas1, but the focus ring is still on the text field
  • After the message box has closed the focus ring is finally on the canvas

Two additional remarks:

  • If you have a break point in the Canvas1.MouseDown event, the focus will change and the focus ring will be drawn around the canvas.
  • Setting AcceptFocus tor true and AcceptTabs to false doesn’t prevent tabbing to the canvas (probably not related to the above).

Workaround: use Me.SetFocus() in Canvas1.MouseDown.

Hi Eli,

thank you for your answer.

Here I have: OS X 10.11.5 / Xojo 2015r1.

I do not have a TextField in that project.
I do not set anything in the default project.
I only add a Canvas (Canvas1: I do not even change its name).
I just paste the code in the newly added Canvas1.MouseDownn event.

Workaround: use Me.SetFocus() in Canvas1.MouseDown.
Does not works here.

I add Focus_Name for debug purposes.

At first, I placed a break point before I add the MsgBox line (I add a Beep and place the break point there) and saw Window1.Focus was on Canvas1.

I reported, using a MsgBox, the name of the control who have the focus after reading the doc page (Xojo 2015r1) Window.Focus where the example was just what I wanted to do !

In the lost project (a project I wrote to display how nice a dropped image in a Canvas is not displayed…), having SetFocus in the MouseDown event may eventually display the dropped image :frowning:

Developing with Xojo nowadays is like Russian roulette: a total disaster !
An internet is close (wifi off), not external hardware connected, no other software fired, nothing.

I even think to search my original Xojo 2015r1 (in case changes have been done to the one in the Archives…) go figure where paranoia will drive people !

[quote=276345:@Emile Schwarz]>Workaround: use Me.SetFocus() in Canvas1.MouseDown.
Does not works here.[/quote]You need to set Canvas1.AcceptFocus to true (either in the IDE or in code in the open event), or SetFocus will of course not work.

Of course, this works, but the problem is it is always ON.

Even after I put a second Canvas (without Canvas2.AcceptFocus to True )… and the code far above…

This turns me to crazy.

And my hair have been cut (drastically) recently and are too small to be taken to pull them off :frowning: or ;-:)…

I may have to take a large nap to clean this mojo business…


Just use Me.SetFocus().