[英]Simulating Enter Key Press sending from a Button on it's Click Event?
Hello I have a problem and don't quite know how to fix it, I'm fairly new to C# programming and object oriented programming in general and only really have limited knowledge of procedural programming because I'm mainly a Dynamics NAV Developer which this solution should also be an AddIn for.您好,我有一个问题,不太知道如何解决它,我对 C# 编程和 object 编程很陌生,而且我对程序编程的了解非常有限,因为我主要是一个动态导航开发人员,这解决方案也应该是一个AddIn。 So I have an Panel as a UserControl, this user Control is populated with some textboxes and buttons, the buttons are arranged to represent a Numpad and are also named like the corresponding Numpad Keys.所以我有一个面板作为用户控件,这个用户控件填充了一些文本框和按钮,这些按钮被安排来代表一个小键盘,并且也像相应的小键盘键一样命名。
So now some words to my actual problem in the following Scenario: I'm entering a textbox typing something in with the Numpad Buttons or with Keyboard and then I want to either press Tab/Enter keys on my keyboard or use the Button Enter to get to the next field, if I'm pressing the actual Keyboard Keys eg "Enter" or "Tabulator" I'm getting focus of the next textbox with following code:所以现在在以下场景中对我的实际问题说一些话:我正在输入一个文本框,使用 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);
}
}
Which is executed in all of the KeyUp Event Handlers of my existing textboxes.这是在我现有文本框的所有 KeyUp 事件处理程序中执行的。 But if I now press Enter this solution won't do it because the next Control in the Form/Panel that i'm using is not the next textbox where it should head to, because I jumped from my textbox that then lost Focus to the Numpad Button Enter which then gets the focus, but how do I manage to jump to the next textbox that has the next greater TabIndex than my recent textbox?但是,如果我现在按 Enter,此解决方案将不会执行此操作,因为我正在使用的表单/面板中的下一个控件不是它应该前往的下一个文本框,因为我从我的文本框跳了出来,然后失去了焦点到Numpad Button Enter 然后获得焦点,但是我如何设法跳转到下一个比我最近的文本框具有下一个更大 TabIndex 的文本框? My textboxes have the following TabIndex:我的文本框有以下 TabIndex:
txtInputField1.TabIndex = 1; txtInputField1.TabIndex = 1;
txtInputField2.TabIndex = 2; txtInputField2.TabIndex = 2;
txtInputField3.TabIndex = 3; txtInputField3.TabIndex = 3;
txtInputField4.TabIndex = 4; txtInputField4.TabIndex = 4;
I also have an field named _recentTextBox to keep track of the recentTextBox which works.我还有一个名为 _recentTextBox 的字段来跟踪最近有效的文本框。
private TextBox _recentTextBox;
Which is then set in each textboxes Leave Event Handler.然后在每个文本框中设置离开事件处理程序。
private void txtInputField1_Leave(object sender, EventArgs e)
{
_recentTextBox = (TextBox)sender;
}
So how I can I achieve this when I enter for example txtInputField1 clicking Enter on the NumPad Button that then the next Control that is selected needs to be txtInputField2 instead of in my situation the next Control that has an greater TabIndex than my Enter Button?那么,当我输入例如 txtInputField1 单击 NumPad 按钮上的 Enter 时,我该如何实现这一点,然后选择的下一个控件需要是 txtInputField2 而不是在我的情况下,下一个控件的 TabIndex 比我的 Enter 按钮大? Thanks in advance and any help would be appreciated.在此先感谢,任何帮助将不胜感激。
Also there's the Layout of my UserControl if it is any help.如果有任何帮助,还有我的 UserControl 的布局。
You could collect your input boxes in the Dictionary<int, TextBox>
mapped by their TabIndex
and have a separate field for the currently selected input field.您可以将输入框收集在由它们的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);
}
In the Enter
event of your input fields, simply set the selectedInputField
value to the sender
value which should be the TextBox
that just got focus.在输入字段的Enter
事件中,只需将selectedInputField
值设置为sender
值,该值应该是刚刚获得焦点的TextBox
。
private void InputField_OnEnter(object sender, EventArgs e)
{
this.selectedInputField = (TextBox)sender;
}
In the KeyUp
event of your input fields and the Click
event of your Enter
button, call the same FocusNextInputField
method which should call the next TextBox
in the line.在输入字段的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();
}
The definition of FocusNextInputField
method should be the following. FocusNextInputField
方法的定义应该如下。
private void FocusNextInputField()
{
int nextInputFieldIndex = this.selectedInputField.TabIndex % inputFieldsTabOrder.Count + 1;
this.selectedInputField = this.inputFieldsTabOrder[nextInputFieldIndex];
this.selectedInputField.Focus();
}
What happens here is that the currently selected input field index gets incremented by one, allowing us to select the next input field in the row.这里发生的是当前选择的输入字段索引增加一,允许我们到 select 行中的下一个输入字段。 Modulo is used in the calculation in order to switch from the last input field to the first one again.计算中使用模数,以便再次从最后一个输入字段切换到第一个输入字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.