Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I simulate a Tab key press when Return is pressed in a WPF application?

Tags:

c#

wpf

keypress

In a WPF application, i have a window that has a lot of fields. When the user uses the TAB key after filling each field, windows understands that it moves on to the next. This is pretty know behavior.

Now what I want to to, is make it simulate the TAB key, when in fact the RETURN gets hit. So in my WPF xaml I added imply KeyDown="userPressEnter"

And in the code behind it:

private void userPressEnter(object sender, KeyEventArgs e)
{
  if (e.Key == Key.Return)
  {
    e.Key = Key.Tab // THIS IS NOT WORKING
  }
}

Now, obviously this is not working. But what I don't know is, how DO I make this work?


EDIT 1 ==> FOUND A SOLUTION

I found something that helped me out =)

private void userPressEnter(object sender, KeyEventArgs e)
{
 if (e.Key == Key.Return)
 {
   TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
   MoveFocus(request);
 }
}

This way the Focus moves on the the next it can find :)

like image 473
Dante1986 Avatar asked Jan 26 '12 21:01

Dante1986


2 Answers

You can look at a post here: http://social.msdn.microsoft.com/Forums/en/wpf/thread/c85892ca-08e3-40ca-ae9f-23396df6f3bd

Here's an example:

private void textBox1_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
                request.Wrapped = true;
                ((TextBox)sender).MoveFocus(request);
            }
        }
like image 123
Nick Bray Avatar answered Oct 20 '22 06:10

Nick Bray


    protected override bool ProcessDialogKey(Keys keyData)
    {
        System.Diagnostics.Debug.WriteLine(keyData.ToString());

        switch (keyData)
        {
            case Keys.Enter:
                SendKeys.Send("{TAB}");
                break;
        }
        base.ProcessDialogKey(keyData);
        return false;
    }
like image 20
ecklerpa Avatar answered Oct 20 '22 06:10

ecklerpa