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.
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 e.g. "Enter" or "Tabulator" I'm getting focus of the next textbox with following code:
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. 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? My textboxes have the following TabIndex:
txtInputField1.TabIndex = 1;
txtInputField2.TabIndex = 2;
txtInputField3.TabIndex = 3;
txtInputField4.TabIndex = 4;
I also have an field named _recentTextBox to keep track of the recentTextBox which works.
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?
Thanks in advance and any help would be appreciated.
Also there's the Layout of my UserControl if it is any help.
You could collect your input boxes in the
Dictionary<int, TextBox>
mapped by theirTabIndex
and have a separate field for the currently selected input field.In the
Enter
event of your input fields, simply set theselectedInputField
value to thesender
value which should be theTextBox
that just got focus.In the
KeyUp
event of your input fields and theClick
event of yourEnter
button, call the sameFocusNextInputField
method which should call the nextTextBox
in the line.The definition of
FocusNextInputField
method should be the following.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. Modulo is used in the calculation in order to switch from the last input field to the first one again.