Textbox.Selectall() works when going through the keyboard, when simulo no

Asked

Viewed 55 times

0

Good staff,

I have a way for 3 Textbox to run when they win 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 stops there is the enter given by the keyboard or by the simulation. When it is through the keyboard it always selects everything that is inside the Textbox, when it is through the simulation of the Key.Enter goes through the top but does not do the SelectAll();

Here’s how I’m simulating the key enter (which is from a number keyboard 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 do the SelectAll to TextBox

What could be wrong here ?

Edit.

Added the Events to the 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:

This is 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();
}
  • After the method call textBoxForNumeric.Focus(), check the property textBoxForNumeric.IsKeyboardFocusWithin, if its value is false, place a call to the method Keyboard.Focus(textBoxForNumeric) in your <ENTER> key press simulator and see if anything changes.

  • That property is the true

  • What’s the code for this event KeyUp that you are routing, when the <ENTER> key is pressed? You can add it to the question?

  • Added, if you put a point break on the events I added, if I enter the keyboard or the numeric keyboard button I created, it will always end up there

  • Seeing the rest of the code now that I realize: Why do you call the method textBoxForNumeric.Focus() within the event GotFocus of control textBoxForNumeric? It is redundant, if this event was triggered means that the control already has the focus, and then would simply select the text, no?

  • I only did it because I saw it somewhere, since I honestly don’t really know what’s going on here, and it was just to test and then didn’t, which I was just going through the variable and doing SelectAll(), and within the textBoxForNumeric It is only to know which of the 3 is going to the text put on the keyboard created in WPF, hence not having an event to see if received Focus or not. Had this, before I was not doing textBoxForNumeric.Focus(); before and still didn’t work

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.