繁体   English   中英

模拟 Enter Key Press 从按钮的 Click 事件上发送?

[英]Simulating Enter Key Press sending from a Button on it's Click Event?

您好,我有一个问题,不太知道如何解决它,我对 C# 编程和 object 编程很陌生,而且我对程序编程的了解非常有限,因为我主要是一个动态导航开发人员,这解决方案也应该是一个AddIn。 所以我有一个面板作为用户控件,这个用户控件填充了一些文本框和按钮,这些按钮被安排来代表一个小键盘,并且也像相应的小键盘键一样命名。
所以现在在以下场景中对我的实际问题说一些话:我正在输入一个文本框,使用 Numpad Buttons 或 Keyboard 输入一些内容,然后我想按键盘上的 Tab/Enter 键或使用 Button Enter 来获取到下一个字段,如果我按下实际的键盘键,例如“Enter”或“Tabulator”,我将使用以下代码获得下一个文本框的焦点:

        private void PerformEnter(object sender, KeyEventArgs e)
    {
        try
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
            {
                this.SelectNextControl((Control)sender, true, true, true, true);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "An error has occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

这是在我现有文本框的所有 KeyUp 事件处理程序中执行的。 但是,如果我现在按 Enter,此解决方案将不会执行此操作,因为我正在使用的表单/面板中的下一个控件不是它应该前往的下一个文本框,因为我从我的文本框跳了出来,然后失去了焦点到Numpad Button Enter 然后获得焦点,但是我如何设法跳转到下一个比我最近的文本框具有下一个更大 TabIndex 的文本框? 我的文本框有以下 TabIndex:

txtInputField1.TabIndex = 1;
txtInputField2.TabIndex = 2;
txtInputField3.TabIndex = 3;
txtInputField4.TabIndex = 4;

我还有一个名为 _recentTextBox 的字段来跟踪最近有效的文本框。

private TextBox _recentTextBox;

然后在每个文本框中设置离开事件处理程序。

    private void txtInputField1_Leave(object sender, EventArgs e)
    {
        _recentTextBox = (TextBox)sender;
    }

那么,当我输入例如 txtInputField1 单击 NumPad 按钮上的 Enter 时,我该如何实现这一点,然后选择的下一个控件需要是 txtInputField2 而不是在我的情况下,下一个控件的 TabIndex 比我的 Enter 按钮大? 在此先感谢,任何帮助将不胜感激。
如果有任何帮助,还有我的 UserControl 的布局。

用户控制布局

您可以将输入框收集在由它们的TabIndex映射的Dictionary<int, TextBox>中,并为当前选定的输入字段设置一个单独的字段。

private Dictionary<int, TextBox> inputFieldsTabOrder = new Dictionary<int, TextBox>();

private TextBox selectedInputField;

public YourForm()
{
    InitializeComponent();

    // Set the first TextBox as the initially selected input field and focus it.
    this.selectedInputField = this.txtInputField1;
    this.selectedInputField.Focus();

    // Collect the other TextBox input fields mapped by TabIndex to your Dictionary.
    this.inputFieldsTabOrder.Add(this.txtInputField1.TabIndex, this.txtInputField1);    
    this.inputFieldsTabOrder.Add(this.txtInputField2.TabIndex, this.txtInputField2);
    this.inputFieldsTabOrder.Add(this.txtInputField3.TabIndex, this.txtInputField3);
    this.inputFieldsTabOrder.Add(this.txtInputField4.TabIndex, this.txtInputField4);   
}

在输入字段的Enter事件中,只需将selectedInputField值设置为sender值,该值应该是刚刚获得焦点的TextBox

private void InputField_OnEnter(object sender, EventArgs e)
{
    this.selectedInputField = (TextBox)sender;
}

在输入字段的KeyUp事件和Enter按钮的Click事件中,调用相同的FocusNextInputField方法,该方法应该调用该行中的下一个TextBox

private void InputField_OnKeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
    {
        this.FocusNextInputField();
    }
}

private void ButtonEnter_OnClick(object sender, EventArgs e)
{
    this.FocusNextInputField();
}

FocusNextInputField方法的定义应该如下。

private void FocusNextInputField()
{
    int nextInputFieldIndex = this.selectedInputField.TabIndex % inputFieldsTabOrder.Count + 1;
    this.selectedInputField = this.inputFieldsTabOrder[nextInputFieldIndex];
    this.selectedInputField.Focus();
}

这里发生的是当前选择的输入字段索引增加一,允许我们到 select 行中的下一个输入字段。 计算中使用模数,以便再次从最后一个输入字段切换到第一个输入字段。

暂无
暂无

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

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