1

If I wanted to just remove focus from something altogether (as in the opposite of saying "Focus();" in code), how would I go about doing it?

Example scenario: I'm KeyboardFocusing a button and using the onclick event, I want to just say "remove focus from this element".

Logan
  • 806
  • 8
  • 17
  • What about focusing another control? – Pikoh Feb 28 '17 at 15:19
  • Possible duplicate of [How to remove the focus from a TextBox in WinForms?](http://stackoverflow.com/questions/1140250/how-to-remove-the-focus-from-a-textbox-in-winforms) – Pikoh Feb 28 '17 at 15:20
  • I was actually doing this previously - i'd focus the 'next' control, but this wasn't ideal for purely visual reasons (my actual goal is to immediately focus the button once more on the next line (this is all for the purpose of fixing a bug in the animation fluidity of the button)). The issue with that is the next control jitters slightly as for a short moment it gains focus and animates accordingly. Ideally, i'd like focus to be destroyed/removed, but a happy alternative would be just focusing the nearest parental contentcontrol (the view's host), as that won't have any visual issues. – Logan Feb 28 '17 at 15:22

5 Answers5

1

You could always use:

Keyboard.ClearFocus();

Rawns
  • 865
  • 4
  • 27
  • This has done it, you're a champion, thanks. I've actually seen this instruction before but couldn't for the life of me remember it. Sadly you've answered my question 'too quickly' so i've got to wait 5minutes before i can check your answer as the accepted one. – Logan Feb 28 '17 at 15:28
  • 1
    Glad to have finally helped. I've received loads of help on here, so nice to give some back. :) – Rawns Feb 28 '17 at 15:55
  • It kills key gesture binding. e.g. setup Ctrl+F for a search dialog, after `Keyboard.ClearFocus()` it won't be triggered before clicking anywhere in the window. – Meow Cat 2012 Aug 06 '19 at 10:13
0

Set the Focusable property on the desired element(s) to false. https://msdn.microsoft.com/en-us/library/system.windows.uielement.focusable(v=vs.110).aspx

Cleptus
  • 3,446
  • 4
  • 28
  • 34
0

In windows always one element has focus. So you can't remove focus but you can divert it to something else. You can set focus to something with no functionality like the form itself

Emad
  • 3,809
  • 3
  • 32
  • 44
  • I think the OP refers to avoid a control from being focused, not sure tho. – Cleptus Feb 28 '17 at 15:22
  • I'm aware of this; so far i know whenever focus is 'destroyed', it jumps like a lightning-rod to the contentpresenter/contentcontrol hosting the given view i'm on, which is absolutely fine. This exchange of focus is only temporary. – Logan Feb 28 '17 at 15:25
0

In .NET Framework 4 just Keyboard.ClearFocus();

However I found that this does not always work... So either give focus to another element altogether or try this code:

// Move to a parent that can take the focus
FrameworkElement parent = (FrameworkElement)yourElement.Parent;
while (parent != null && parent is IInputElement && ! ((IInputElement)parent).Focusable)
{
    parent = (FrameworkElement)parent.Parent;
}

DependencyObject scope = FocusManager.GetFocusScope(yourElement);
FocusManager.SetFocusedElement(scope, parent as IInputElement);
Leo
  • 5,013
  • 1
  • 28
  • 65
-3

you may not be able to directly remove focus but! you can switch focus to a different application

[DllImport("USER32.dll")]
static extern bool SetForegroundWindow (IntPtr hWnd);

private void SwitchFocusTo(Process diffProcess)
{
    SetForegroundWindow(diffProcess.MainWindowHandle);
}
Noah Reinagel
  • 402
  • 3
  • 6