繁体   English   中英

按 Enter 移动到下一个控件

[英]Press Enter to move to next control

我在 WinForm 上有几个 TextBox。 当按下 Enter 键时,我希望焦点移动到下一个控件? 每当文本框获得控制权时,它也会选择文本,以便任何编辑都将替换当前文本。

做这个的最好方式是什么?

Tab as Enter:创建一个继承文本框的用户控件,覆盖KeyPress方法。 如果用户按 Enter,您可以调用SendKeys.Send("{TAB}")System.Windows.Forms.Control.SelectNextControl() 请注意,您可以使用KeyPress事件实现相同的效果。

聚焦整个文本:同样,通过覆盖或事件,以GotFocus事件为目标,然后调用TextBox.Select方法。

在 C# 中使用SelectNextControl 的几个代码示例。

当按下ENTER时,第一个移动到下一个控件。

    private void Control_KeyUp( object sender, KeyEventArgs e )
    {
        if( (e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return) )
        {
            this.SelectNextControl( (Control)sender, true, true, true, true );
        }
    }

第二个使用向上向下箭头在控件之间移动。

    private void Control_KeyUp( object sender, KeyEventArgs e )
    {
        if( e.KeyCode == Keys.Up )
        {
            this.SelectNextControl( (Control)sender, false, true, true, true );
        }
        else if( e.KeyCode == Keys.Down )
        {
            this.SelectNextControl( (Control)sender, true, true, true, true );
        }
    }

参见 MSDN SelectNextControl 方法

在 KeyPress 事件中,如果用户按下 Enter,则调用

SendKeys.Send("{TAB}")

在接收焦点时实现自动选择文本的最好方法是使用以下覆盖在您的项目中创建 TextBox 的子类:

Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs)
    SelectionStart = 0
    SelectionLength = Text.Length
    MyBase.OnGotFocus(e)
End Sub

然后在所有表单上使用此自定义 TextBox 代替 WinForms 标准 TextBox。

您可以在KeyPress上放置KeyPress处理程序,并查看使用了哪个键。

要处理文本选择,请在GotFocus事件上放置一个处理程序。

您可能还需要考虑如何(或如果需要)处理多行文本框。

这可能有帮助:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    //
    // Detect the KeyEventArg's key enumerated constant.
    //
    if (e.KeyCode == Keys.Enter)
    {
        MessageBox.Show("You pressed enter! Good job!");
    }
}
private void txt_invoice_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
            txt_date.Focus();
    }

    private void txt_date_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
            txt_patientname.Focus();
    }

}

您也可以为此编写自己的控件,以防您想更频繁地使用它。 假设您在一个网格中有多个文本框,它看起来像这样:

public class AdvanceOnEnterTextBox : UserControl
{

    TextBox _TextBox;
    public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(AdvanceOnEnterTextBox), null);
    public static readonly DependencyProperty InputScopeProperty = DependencyProperty.Register("InputScope", typeof(InputScope), typeof(AdvanceOnEnterTextBox), null);


    public AdvanceOnEnterTextBox()
    {
        _TextBox = new TextBox();
        _TextBox.KeyDown += customKeyDown;
        Content = _TextBox;

    }


    /// <summary>
    /// Text for the TextBox
    /// </summary>
    public String Text
    {
        get { return _TextBox.Text; }
        set { _TextBox.Text = value; }
    }


    /// <summary>
    /// Inputscope for the Custom Textbox
    /// </summary>
    public InputScope InputScope
    {
        get { return _TextBox.InputScope; }
        set { _TextBox.InputScope = value; }
    }


    void customKeyDown(object sender, KeyEventArgs e)
    {
        if (!e.Key.Equals(Key.Enter)) return;

        var element = ((TextBox)sender).Parent as AdvanceOnEnterTextBox;
        if (element != null)
        {
            int currentElementPosition = ((Grid)element.Parent).Children.IndexOf(element);
            try
            {
                // Jump to the next AdvanceOnEnterTextBox (assuming, that Labels are inbetween).
                ((AdvanceOnEnterTextBox)((Grid)element.Parent).Children.ElementAt(currentElementPosition + 2)).Focus();
            }
            catch (Exception)
            {
                // Close Keypad if this was the last AdvanceOnEnterTextBox
                ((AdvanceOnEnterTextBox)((Grid)element.Parent).Children.ElementAt(currentElementPosition)).IsEnabled = false;
                ((AdvanceOnEnterTextBox)((Grid)element.Parent).Children.ElementAt(currentElementPosition)).IsEnabled = true;
            }
        }
    }
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == (Keys.Enter))
        {
            SendKeys.Send("{TAB}");
        }

        return base.ProcessCmdKey(ref msg, keyData);
    }

转到设计表单和视图-> 选项卡(如图所示)订购然后您订购了所有控件[就是这样] 在此处输入图片说明

尝试使用:

SendKeys.Send("{TAB}")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM