【问题标题】:Simulating Enter Key Press sending from a Button on it's Click Event?模拟 Enter Key Press 从按钮的 Click 事件上发送?
【发布时间】:2021-03-10 05:33:23
【问题描述】:

您好,我有一个问题,不知道如何解决,我对 C# 编程和面向对象编程还很陌生,而且对过程编程的了解非常有限,因为我主要是 Dynamics NAV此解决方案也应为其加载项的开发人员。 所以我有一个面板作为用户控件,这个用户控件填充了一些文本框和按钮,这些按钮被安排来代表一个小键盘,并且也像相应的小键盘键一样命名。
所以现在对我在以下场景中的实际问题说几句话: 我正在输入一个使用 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 时,我该如何实现这一点按钮? 提前致谢,我们将不胜感激。
如果有任何帮助,还有我的 UserControl 的布局。

【问题讨论】:

    标签: c# windows-forms-designer microsoft-dynamics-nav


    【解决方案1】:

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

    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();
    }
    

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

    【讨论】:

    • 感谢您的详细帮助,它成功了。
    猜你喜欢
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 2012-10-10
    • 2018-06-19
    • 1970-01-01
    • 2012-09-24
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多