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).
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.
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
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 McDonalds at debug time and the sound is always off there for obvious reasons.
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).
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
What about MouseDown / MouseUp ?
(Two Events for the price of one )
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.
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.
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.
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.