Multiple component events in only one method

5

I have several events of my textbox, the problem is that I have a 30 in my form. I would like to know if there is any way for me to improve my code, reduce it by creating only one method to handle all of those events.

Three TextBox sample events:

    private void textEmpPrefixo_KeyUp(object sender, KeyEventArgs e)
    {
        var elemento = e.OriginalSource as UIElement;

        if (e.Key == Key.Down)
        {
            elemento.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
        }

        if (e.Key == Key.Up)
        {
            elemento.MoveFocus(new TraversalRequest(FocusNavigationDirection.Previous));
        }
    }

    private void textEmpTelefones_KeyUp(object sender, KeyEventArgs e)
    {
        var elemento = e.OriginalSource as UIElement;

        if (e.Key == Key.Down)
        {
            elemento.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
        }

        if (e.Key == Key.Up)
        {
            elemento.MoveFocus(new TraversalRequest(FocusNavigationDirection.Previous));
        }
    }

    private void textEmpObs2_KeyUp(object sender, KeyEventArgs e)
    {
        var elemento = e.OriginalSource as UIElement;

        if (e.Key == Key.Down)
        {
            elemento.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
        }

        if (e.Key == Key.Up)
        {
            elemento.MoveFocus(new TraversalRequest(FocusNavigationDirection.Previous));
        }
    }

I use them to change focus as they use the up or down arrow I use C # wpf technology.

Thank you in advance (:

    
asked by anonymous 22.05.2015 / 13:18

1 answer

2

You can have only one handler and the TextBox in the KeyUp event call the handler.

private void MudarFoco_keyUp(object sender, KeyEventArgs e)
{
    var elemento = e.OriginalSource as UIElement;

    if(e.Key == Key.Down)
        elemento.MoveFocus(New TraversalRequest(FocusNavigationDirection.Next));

    if(e.Key == Key.Up)
       elemento.MoveFocus(newTraversalRequest(FocusNavigationDirection.Previous));
}

and in the event of TextBox ;

KeyUp="MudarFoco_KeyUp";
    
22.05.2015 / 13:33