Hello in my WinForms application i have this below program which helps to Go to next text box when enter key is pressed , But i am facing an issue here .
When I press enter key after I type something or without typing anything in first textbox it is going to next text box.
But if I type something accidentally and hit backspace to erase the first textbox content and if I press enter key then it is not moving to next textbox , Kindly help how to solve this issue.
private void Form1_Shown(object sender, EventArgs e)
{
try
{
foreach (Control control in Controls)
{
if (control is TextBox textBox)
{
textBox.KeyDown += (s, ea) =>
{
if (ea.KeyCode != Keys.Enter) return;
ea.SuppressKeyPress = true;
SelectNextControl(ActiveControl, true, true, true, true);
};
}
else if (control is ComboBox comboBox)
{
comboBox.KeyDown += (s, ea) =>
{
if (ea.KeyCode != Keys.Enter) return;
ea.SuppressKeyPress = true;
SelectNextControl(ActiveControl, true, true, true, true);
};
}
else if (control is DateTimePicker dateTimePicker)
{
dateTimePicker.KeyDown += (s, ea) =>
{
if (ea.KeyCode != Keys.Enter) return;
ea.SuppressKeyPress = true;
SelectNextControl(ActiveControl, true, true, true, true);
};
}
}
}
catch (Exception ex)
{
MessageBox.Show("Enter Failed:" + ex.Message.ToString(), "Enter",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}