Canvas: set / clear Focus in MouseDown

Xojo 2014r2.1
OS X 10.9.4
Windows XP

In a Canvas.MouseDown, I placed that code:

[code] // Set / Clear the Focus to the Canvas
If Self.Focus = Nil Then
// Yes, so set the Focus to this Canvas
Me.SetFocus

// Debug: developer report
TA_Info.Text = "The Focus is set to the Canvas (Me.SetFocus)."

Else
// Clear the focus (no Control have the Focus)
Self.Focus = Nil

// Debug: developer report
TA_Info.Text = "There is no Focus."

End If

// Redraw the Canvas contents
Self.Refresh
Self.Invalidate[/code]

TA_Info is a TextArea and I place text report only for the debug purpose. So I know the code runs where due, but the Focus is not set as intended.

What is wrong in it ?

BTW: this appears in both IDE --> Run and StandAlone application / OS X & WIndows XP stand alone.

[Third attempt to post this; this is the good one !]

[quote=122734:@Emile Schwarz]// Clear the focus (no Control have the Focus)
Self.Focus = Nil[/quote]

You should try

ClearFocus

instead

Merci Michel.

ClearFocus: pas trouv (not found).

Window.Focus:
Gets or sets the RectControl in the window that has the focus. If no control has the focus, this property is Nil.

So, if I set Window.Focus to Nil, I clear the focus. That is why I do that. Am I wrong ?

Check the docs for ClearFocus - it is a method of the Window class.

Self.ClearFocus()

Thanks Eli,

it have a draw back on windows the application will no more receive key down (I just read it).

Sorry, was wrong with my statement. ClearFocus is a global method. Have you tried calling ClearFocus() and on the next line Self.SetFocus() so the focus is set to the window?

Not yet, will do as soon as I can. Thanks

[quote=122752:@Emile Schwarz]ClearFocus: pas trouvé (not found).

Window.Focus:
Gets or sets the RectControl in the window that has the focus. If no control has the focus, this property is Nil.

So, if I set Window.Focus to Nil, I clear the focus. That is why I do that. Am I wrong ?[/quote]

Self.Focus does not work. ClearFocus usually does, although I have seen sometimes a TextField with TabIndex 0 steal the focus.
http://documentation.xojo.com/index.php/ClearFocus

My code adds the focus, then add the user reports as appropriate, but does not change the Focus status. The used code have been greatly modified before I share it.

Note that I forgot about ClearFocus probably because we do not have to use it anymore ?

I even checked:

[code]If Self.Focus.Name = Me.Name Then
// Me have the focus, so clear it
Self.Focus = Nil

Else
// Me does not have the focus, so set it
Self.Focus = Me
End If[/code]

From memory, so the code may not work, but you see what I meant.

[quote=122801:@Emile Schwarz]My code adds the focus, then add the user reports as appropriate, but does not change the Focus status. The used code have been greatly modified before I share it.

Note that I forgot about ClearFocus probably because we do not have to use it anymore ?

I even checked:

[code]If Self.Focus.Name = Me.Name Then
// Me have the focus, so clear it
Self.Focus = Nil

Else
// Me does not have the focus, so set it
Self.Focus = Me
End If[/code]

From memory, so the code may not work, but you see what I meant.[/quote]

Apparently, what you want to do is each time you click on the canvas, it sets or remove the focus.

I tried to run your code, and encountered several issues :

  • Self.Focus = Nil does not remove the focus from the canvas. Clearfocus does.
  • When no control has the focus, Self.Focus.Name becomes nil which triggers a nil exception
  • Self.Focus = Me does not set the focus to the canvas. Me.Setfocus does.

Instead of Self.Focus.Name, you may want to use a boolean window property to keep track of the current canvas focus state.

Emile, have you solved your problem ?

Not yet. I “released” the project (ArcShape Demo), so you can test from it. Check on another Conversation.

BTW: the code you refer in your previous message is one I left for the one at the top of this page. You wrote excatly why I trashed it.

Have a nice day.

Whatever :confused: