Set the Focus to a Canvas by Click

I build a set of Canvas into a window by code, then populates them with tamplates images.

The next step is to allow the user to choose one of them by click (I already set the selection by Tab).

In .MouseDown, I wrote SetFocus, but nothing appears on screen.
When I used (bad idea) to issue a MsgBox to report the # of the focused Canvas, I get the user report (the focus). But it was just for debug, I already removed it.

Xojo 2015r1 / El Capitan, but Linux and Windows later).

Ideas ?

In the canvas inspector try enabling both Accept Focus and Use Focus Ring. Also use system.debug.log instead of a msgbox.

I could be wrong, but I think when a Canvas control has the focus, the GotFocus Event will fire, which you then use to draw your own highlight in the Paint Event. Then use the LostFocus Event to remove whatever you drew, again using the Paint Event.

And as @Tanner Lee says, you need to set the Focus properties first in the Inspector to get the Got/Lost Focus events to work.

That’s my understanding anyway, I hope that helps a bit.

Thanks for the advices.

UseFocusRing and AcceptFocus are set to True.

The code I put in GotFocus draw images in two (different) Canvas as a user report and that event fires when I press the \Tab key, not with a click. I also display the name of the loaded / displayed image (for the Focused Canvas) whet \Tab is depressed, not when I click in it.

MouseDown if fired (I just add a beep there and it chimes in).
GotFocus do not react, no beep, no Debugger Break.

So some weird “non thing” is at work here :frowning:

About the MsgBox use: I already wrote [quote=467498:@Emile Schwarz]I already removed it[/quote]. I could used a Beep, but I was at McDonald’s at debug time and the sound is always off there for obvious reasons.

Does not works. System.DebugLog is far better :wink:

Too few code to share a project:

Place a Canvas in a brand new project (outside of Window1),
Set the MemberOf to Canvas1,
Add it a Paint event with this code:

Sub Paint(index as Integer, g As Graphics, areas() As REALbasic.Rect) g.ForeColor = cmy(0.0,0.0,0.20) g.FillRect 0,0, Me.Width,Me.Height End Sub

Add it a GotFocus event with this code:

Sub GotFocus(index as Integer) System.DebugLog "I’ve Got The Focus !" End Sub

Window1.Open Event:

[code]Sub Open()
Dim Ctrl_Cnt As Integer = 3
// Dim Ctrl_cvs As Canvas
Dim Loop_Idx As Integer

For Loop_Idx = 1 To Ctrl_Cnt
Dim Ctrl_cvs As Canvas

Ctrl_cvs = New Canvas1
Ctrl_cvs.Left = 20 + (150 * (Loop_Idx-1))
Ctrl_cvs.Top  = 20

Next

Self.Invalidate

System.DebugLog "Number of Controls in the window: " + Str(Self.ControlCount)

Beep
End Sub[/code]

Open the SystemLog pane,
Run,
Click once in each of the three Canvas…
Press the Tab key thrice…

You got no answer for the click,
You got three answers for the Tab Key…

Remember: Xojo 2015r1 (so API1).

Add me.setfocus in the canvas’ MouseDown event. This will trigger both the focus ring and the GetFocus event. Note that I’m doing this in 2019R1.1 (API1).

Edit: works in 16R3

On macOS, make sure you have the accessibility settings set such that controls other than text fields and buttons can receive focus.

Thanks Greg, but this change nothing (here).

In fact, the click select the last Canvas of the array (but do not display it). I know that when I press a Tab: the next Control have the Focus (or the previous if Shift-Tab).

How naive I was thinking “this is super easy”… incredible :wink:

I DO NOT UNDERSTAND:

I added a Label (L_Info) and put in the Canvas (The one I clone) MouseDown:

L_Info.Text = "Selected Canvas: " + Str(Index) + "."

Now, I got the Focus Ring !

PS: I commented the added line and delete the Label: Is till have the Focus ring. Extremely strange !
This is in my main project, not in the test one.

Test project: does change nothing ! (I do not saved the project changes).

In the test project (still without saving the changes):

Me.SetFocus // Does not works
Me.SetFocus() // Works

And I disabled the Accessibility thing. :frowning:

Before that, I loaded 19r1.1, then 19.r3 (all three at the same time, with FireFox and EyeTV running)…

I feel hungry, where’s my hat ?
(at 19:32, it start to be time for some eating ! I’ll be back by tomorrow morning).

@Emile Schwarz : I uploaded a working sample here - https://www.dropbox.com/s/5vr3b8ykpjlu7ut/CanvasFocus.xojo_binary_project?dl=1

Hope it helps…

i am missing also the Action Event in the Canvas, maybe is it responsible for the GotFocus

There is no Action Event in Canvas.

Or if there is one, where did you see it in the documentation ?

PS: @ Tanner… the example works fine, thank you.

i wrote “i am missing also the Action Event”
i meant it is not there in xojo.

Sorry.

What about MouseDown / MouseUp ?
(Two Events for the price of one :wink: )

That said, now that I get a good night, I understand why I had that trouble earlier with Canvas selection: I believed it was my fault why I was not able to select a Canvas (for Copy or Paste), in another (but relates), older project. :frowning:

action event is device neutral. yes you can simulate this action event but i meant to have it consistently for all ui elements.
and i guess because its missing it also get no GetFocus event.

[quote=467670:@Tanner Lee]@Emile Schwarz : I uploaded a working sample here - https://www.dropbox.com/s/5vr3b8ykpjlu7ut/CanvasFocus.xojo_binary_project?dl=1

Hope it helps…[/quote]
Thank you Tanner. You example works great.

But because I’m still a little gun-shy about using Control Sets, I was tinkering this evening and thought I would make a version based on a sub-classed Canvas.

Happily, and strangely enough (because I wasn’t expecting it to work the first time), my example seems to work without setting Keyboard access on macOS.

My screen-shot shows using a mouse click, but you can also just Tab to each Canvas and click your space-bar to trigger the custom ButtonPressed Event definition.

Download CanvasFocusRingExample.xojo_binary_project

Note: I built this using 2019r3 on High Sierra and haven’t tested it on Windows or a higher macOS version, yet.

Thank you everyone for your tips and I hope my example can also help someone. Thanks.

Thank you Scott. I do not had that idea: it seemed so simple at first !

You’re very welcome Emile.

I also thought it should be quite simple, based on some other posts that I’d read. But in the end it was not too hard, once I’d worked the problem. The example by @Tanner Lee was a great help!

Of course I forgot Windows doesn’t have a Focus Ring (it was late last night when I did this), but at least on Windows the Canvas does appear to receive the Control Focus when tabbing and respond to the space-bar and such. I guess on Windows we’ll have to paint our own highlight border, as others have suggested.