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

后端 未结 7 2041
悲哀的现实
悲哀的现实 2021-01-01 16:37

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. Th

相关标签:
7条回答
  • 2021-01-01 16:40

    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);
                }
            }
    
    0 讨论(0)
  • 2021-01-01 16:42

    Use Method SelectNextControl of your Form

    0 讨论(0)
  • 2021-01-01 16:43

    I think you should use that to simulate TAB :

    SendKeys.Send("{TAB}");
    

    Instead of

    e.Key = Key.Tab
    

    Sources : http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send.aspx

    0 讨论(0)
  • 2021-01-01 16:50

    How about make SendKeys Class Working like Winforms.SendKeys

    https://michlg.wordpress.com/2013/02/05/wpf-send-keys/

    public static class SendKeys
    {
        public static void Send(Key key)
        {
            if (Keyboard.PrimaryDevice != null) {
                if (Keyboard.PrimaryDevice.ActiveSource != null) {
                    var e1 = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, key) { RoutedEvent = Keyboard.KeyDownEvent };
                    InputManager.Current.ProcessInput(e1);
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-01 16:52
        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;
        }
    
    0 讨论(0)
  • 2021-01-01 16:53

    I think the best solution is:

    var ue = e.OriginalSource as FrameworkElement;
    
    if (e.Key == Key.Return)
    {
        e.Handled = true;
        ue.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
    }
    
    0 讨论(0)
提交回复
热议问题