2

I have the requirement that whenever a TextBox gets Focus (via Touch, Mouse, Keyboard) that all Text should be selected.

I tried with the GotFocus event and with the OnMouseDown/OnMouseUp (for mouse) events:

if (this.SelectionLength == 0)
{
    this.Focus();
    this.SelectAll();
}

but this is not working in all cases.

Sometimes I need to click twice into a TextBox, sometimes not.

Is the a definitive answer here?

Florian
  • 1,827
  • 4
  • 30
  • 62
  • 2
    Do you really want mouse clicks to focus all the contents of the textbox? This effectively makes it so you cannot change a single letter of a word, only retype the whole thing. – Kevin DiTraglia Oct 10 '13 at 12:15
  • 1
    pretty much definitive http://stackoverflow.com/questions/660554/how-to-automatically-select-all-text-on-focus-in-wpf-textbox – Rohit Oct 10 '13 at 12:16
  • @KevinDiTraglia yes, sadly. – Florian Oct 10 '13 at 12:19
  • Post the other bits of code please. –  Oct 10 '13 at 12:20
  • Sorry for the duplicate question. I can confirm that the linked question is identical (but bad to find via Google) – Florian Oct 10 '13 at 12:22
  • While you write a title for your Question SO would have suggested this question to you :) – Sriram Sakthivel Oct 10 '13 at 12:29
  • @SriramSakthivel, maybe you should have quickly checked your assumption, before commenting... I just did (out of curiosity) and that other post was *not* suggested, although strangely, it *was* in the close dialog. – Sheridan Oct 10 '13 at 12:39
  • @Sheridan Me too tried out of curiosity. Try this(Textbox SelectAll wpf) SO suggests that question – Sriram Sakthivel Oct 10 '13 at 12:48
  • @SriramSakthivel, that is strange indeed... maybe *I* should have checked *that* before *I* commented. :) – Sheridan Oct 10 '13 at 12:53

1 Answers1

0

We have two types of focus availables in WPF logical and Keyboard. for you can use eighter of them

For KeyBoard.

Keyboard.Focus(myControl);   

For Logical

 FocusManager.SetFocusedElement(this, myControl);
JSJ
  • 5,653
  • 3
  • 25
  • 32