# Canvas: set / clear Focus in MouseDown

**URL:** <https://forum.xojo.com/t/canvas-set-clear-focus-in-mousedown/18732>\
**Category:** General\
**Created:** [August 20, 2014, 8:17am UTC](https://forum.xojo.com/t/canvas-set-clear-focus-in-mousedown/18732 "2014-08-20T08:17:00Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [August 20, 2014, 8:17am UTC](https://forum.xojo.com/t/canvas-set-clear-focus-in-mousedown/18732/1 "2014-08-20T08:17:00Z")

</div>

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 !]

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [August 20, 2014, 8:24am UTC](https://forum.xojo.com/t/canvas-set-clear-focus-in-mousedown/18732/2 "2014-08-20T08:24:19Z")

</div>

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

You should try

```auto
ClearFocus
```

instead

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [August 20, 2014, 8:56am UTC](https://forum.xojo.com/t/canvas-set-clear-focus-in-mousedown/18732/3 "2014-08-20T08:56:35Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![Eli\_Ott](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@Eli\_Ott](https://forum.xojo.com/u/Eli_Ott)\
**Post date:** [August 20, 2014, 9:05am UTC](https://forum.xojo.com/t/canvas-set-clear-focus-in-mousedown/18732/4 "2014-08-20T09:05:02Z")

</div>

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

```auto
Self.ClearFocus()
```

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [August 20, 2014, 9:30am UTC](https://forum.xojo.com/t/canvas-set-clear-focus-in-mousedown/18732/5 "2014-08-20T09:30:20Z")

</div>

Thanks Eli,

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

---

<div class="post-metadata">

**Author:** ![Eli\_Ott](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@Eli\_Ott](https://forum.xojo.com/u/Eli_Ott)\
**Post date:** [August 20, 2014, 10:00am UTC](https://forum.xojo.com/t/canvas-set-clear-focus-in-mousedown/18732/6 "2014-08-20T10:00:23Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [August 20, 2014, 10:43am UTC](https://forum.xojo.com/t/canvas-set-clear-focus-in-mousedown/18732/7 "2014-08-20T10:43:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [August 20, 2014, 10:50am UTC](https://forum.xojo.com/t/canvas-set-clear-focus-in-mousedown/18732/8 "2014-08-20T10:50:29Z")

</div>

[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](http://documentation.xojo.com/index.php/ClearFocus)

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [August 20, 2014, 12:11pm UTC](https://forum.xojo.com/t/canvas-set-clear-focus-in-mousedown/18732/9 "2014-08-20T12:11:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [August 20, 2014, 12:37pm UTC](https://forum.xojo.com/t/canvas-set-clear-focus-in-mousedown/18732/10 "2014-08-20T12:37:49Z")

</div>

[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.

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [August 21, 2014, 9:07am UTC](https://forum.xojo.com/t/canvas-set-clear-focus-in-mousedown/18732/11 "2014-08-21T09:07:12Z")

</div>

Emile, have you solved your problem ?

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [August 21, 2014, 11:44am UTC](https://forum.xojo.com/t/canvas-set-clear-focus-in-mousedown/18732/12 "2014-08-21T11:44:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [August 21, 2014, 11:55am UTC](https://forum.xojo.com/t/canvas-set-clear-focus-in-mousedown/18732/13 "2014-08-21T11:55:13Z")

</div>

Whatever 😕

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 5:22pm UTC](https://forum.xojo.com/t/canvas-set-clear-focus-in-mousedown/18732/14 "2020-10-29T17:22:59Z")

</div>


