TextBox.SelectAll () works when it is through the keyboard, when it does not

0

Good people,

I have a method for 3 TextBox to run when they gain focus:

private void NumericKeyboardTextBox_GotFocus(object sender, RoutedEventArgs e)
{
    e.Handled = true;
    this.textBoxForNumeric = sender as TextBox;
    this.textBoxForNumericFirstTime = true;
    this.textBoxForNumeric.Focus();
    this.textBoxForNumeric.SelectAll();
}

By a point break it is either the enter given by the keyboard or the simulation. When it is by the keyboard it always selects everything inside the TextBox, when it is by simulation of Key.Enter it passes through the method above but does not SelectAll();

Here's how I'm simulating the key enter (which is a keyboard number I made in WPF)

Keyboard.FocusedElement.RaiseEvent(new KeyEventArgs(Keyboard.PrimaryDevice, PresentationSource.FromVisual(this.textBoxForNumeric), 0, Key.Enter)
{
    RoutedEvent = Keyboard.KeyUpEvent
});

By pressing the enter button on the keyboard I made, it passes the method but does not SelectAll a TextBox

What could be wrong here?

Edit.

Added events to KeyUp

#region Events for KeyUp for the TextBoxPrice & TextBoxNewQtd
private void textBoxBarCodToSearch_KeyUp(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        if (!String.IsNullOrEmpty(this.textBoxBarCodToSearch.Text))
        {
            FindProdForEventsDataGridAllProds(this.textBoxBarCodToSearch.Text);
        }
    }
}

private void textBoxPrice_KeyUp(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        this.textBoxNewQtd.Focus();
    }
}

private void textBoxNewQtd_KeyUp(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        BindingExpression binding = this.textBoxNewQtd.GetBindingExpression(TextBox.TextProperty);
        binding.UpdateSource();
        AddNewProdToDoc();
    }
}
#endregion

Edit 2:

That was how the method was initially NumericKeyboardTextBox_GotFocus

private void NumericKeyboardTextBox_GotFocus(object sender, RoutedEventArgs e)
{
    // Passar para a variavel para saber qual o texto a modificar com o teclado virtual criado na WPF
    // As 2 linhas abaixo é como tinha inicialmente, o resto foi adicionado/removido para ver se resolvia o problema
    this.textBoxForNumeric = sender as TextBox;
    (sender as TextBox).SelectAll();
}
    
asked by anonymous 17.10.2018 / 16:58

0 answers