3

I have some concept question here. I know how to select all text in TextBox or in PasswordBox. Via GotKeyboardFocus and PreviewMouseLeftButtonDown events, you know. This works fine.

XAML:

PreviewMouseLeftButtonDown="PasswordOnPreviewMouseDown"
GotKeyboardFocus="SelectAllPassword"

CodeBehind

private void SelectAllPassword(Object sender, RoutedEventArgs e)
{
    var pb = (sender as PasswordBox);
    if (pb != null)
        pb.SelectAll();
}

private void PasswordOnPreviewMouseDown(Object sender, MouseButtonEventArgs e)
{
    var pb = (sender as PasswordBox);
    if (pb != null)
        if (!pb.IsKeyboardFocusWithin)
        {
            e.Handled = true;
            pb.Focus();
        }
}

But question is - why this doesn't work?

XAML:

PreviewMouseLeftButtonDown="PasswordOnPreviewMouseDown"

CodeBehind:

private void PasswordOnPreviewMouseDown(Object sender, MouseButtonEventArgs e)
{
    _txtPassword.SelectAll();
    e.Handled = true;
}

Where _txtPassword - TextBox or PasswordBox control. So why I'm enforsed to Focus text control?

monstr
  • 1,680
  • 1
  • 25
  • 44
  • Hmm, cause I tested these both approaches – monstr May 13 '14 at 04:42
  • I know you did, but my question was how did you test it and how did the second approach fail the test ? – franssu May 13 '14 at 07:12
  • What means "How did it fail"? O_o Make your test-project in VS and test it yourself. All required code for testing I placed here. – monstr May 13 '14 at 07:23
  • In other words : how do you check if the text has been selected or not ? – franssu May 13 '14 at 07:56
  • Visually. Text does't highlighted by blue color – monstr May 13 '14 at 08:59
  • Are you really sure it implies it's not selected ? It does not. Text is highlighted if it's selected AND focused. You can try playing with focus using the tab key and see it for yourself. – franssu May 13 '14 at 09:09
  • Got you. You are right. But what does mean `SelectAll()` then? it seems `SelectAll()` doesn't have any sense without control to be focused? – monstr May 13 '14 at 09:33
  • It means it selects all the text inside the TextBox, and it does only that. It makes sense, for example if you have been binding on the SelectedText property. – franssu May 13 '14 at 09:51
  • Thx. I think your frase `Text is highlighted if it's selected AND focused.`- is answer on my question. You can form it as answer and I will accept it – monstr May 13 '14 at 10:07
  • Did u try to remove `e.Handled=true;` line from the code which is not working and checked? – S2S2 May 13 '14 at 10:33
  • Does this answer your question? [Calling Select() on WPF text box doesn't do anything](https://stackoverflow.com/questions/487141/calling-select-on-wpf-text-box-doesnt-do-anything) – StayOnTarget Jan 13 '21 at 18:09

1 Answers1

3

Actually, the selection is working.

You may feel that the text is not selected because it's not visually highlighted, but that's because the TextBox is not focused.

Try giving focus to your TextBox with the Tab key, you'll see the whole text highlighted.

franssu
  • 2,422
  • 1
  • 20
  • 29