WPF和WindowsForm下的按下Enter跳转下一个控件通用方法

WP下按下回车(enter)跳转下一个控件 上代码:

protected override void OnKeyDown(KeyEventArgs e)
        {

            if (e.Key == Key.Enter)
            {
                // MoveFocus takes a TraveralReqest as its argument.
                TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);

                // Gets the element with keyboard focus.
                UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;

                // Change keyboard focus.
                if (elementWithFocus != null)
                {
                    elementWithFocus.MoveFocus(request);
                }
                e.Handled = true;
            }
            base.OnKeyDown(e);
        }

在窗体里写上就可以了。

 

WindowsForm下的按下回车(Enter)跳转到下一个控件

 /// <summary>
        /// 方法一:实现按下回车跳到下一个控件(不论是什么控件,如果需要可以在里面加上对控件类型的判断)
        /// </summary>
        /// <param name="e"></param>
        protected override void OnKeyDown(KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Enter)
            {
                int i = 0;
                foreach (Control c in this.Controls)
                {
                    if (c.Focused)
                    {
                        i = c.TabIndex;
                        break;
                    }
                }
                foreach (Control c in this.Controls)
                {
                    if (c.TabIndex == (i + 1))
                    {
                        c.Focus();
                        break;
                    }
                }

            }
        }
        /// <summary>
        /// 方法二:灵活方便,可以通过方法中的参数控制是否要进入控件的子控件中,是否忽略tobstop的设置。
        /// </summary>
        /// <param name="e"></param>
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            //base.OnKeyPress(e);
            if (e.KeyChar == (char)13)
            {
                e.Handled = true;
                this.SelectNextControl(this.ActiveControl, true, true, true, false);
            }
        }
        /// <summary>
        /// 方法三:直接将enter按键转化为tab按键
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            MessageBox.Show("");
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
                e.Handled = true;
            }
        }

具体写在哪个事件里面,以个人情况决定。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的示例代码,可以用于在WPF中设计同一个解决方案下的不同界面。 首先,在解决方案中添加两个窗口:MainWindow.xaml和SecondWindow.xaml,用于实现不同界面的。在MainWindow.xaml中,我们可以添加一个Button件,用于触发操作: ```XAML <Window x:Class="WpfApp1.MainWindow" ... Title="Main Window" Height="350" Width="525"> <Grid> <Button Content="Open Second Window" Width="150" Height="30" Margin="5" Click="Button_Click"/> </Grid> </Window> ``` 在代码中,我们可以使用Button的Click事件来打开SecondWindow窗口。以下是示例代码: ```C# private void Button_Click(object sender, RoutedEventArgs e) { // 打开SecondWindow窗口 SecondWindow secondWindow = new SecondWindow(); secondWindow.Show(); // 关闭当前窗口 this.Close(); } ``` 在SecondWindow.xaml中,我们可以添加一个Button件,用于返回到MainWindow界面: ```XAML <Window x:Class="WpfApp1.SecondWindow" ... Title="Second Window" Height="350" Width="525"> <Grid> <Button Content="Go Back to Main Window" Width="200" Height="30" Margin="5" Click="Button_Click"/> </Grid> </Window> ``` 在代码中,我们可以使用Button的Click事件来返回到MainWindow界面。以下是示例代码: ```C# private void Button_Click(object sender, RoutedEventArgs e) { // 打开MainWindow窗口 MainWindow mainWindow = new MainWindow(); mainWindow.Show(); // 关闭当前窗口 this.Close(); } ``` 在这个示例代码中,我们使用了两个不同的窗口来实现不同界面的。当用户在MainWindow界面点击按钮时,打开SecondWindow界面;当用户在SecondWindow界面点击按钮时,返回到MainWindow界面。 希望这个简单的示例代码对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值