开发人员技术 | Windows 窗体
一组用于开发图形用户界面的 .NET Framework 托管库。
您好,在我的WinForms应用程序中,我有这个下面的程序,它有助于在按下回车键时转到下一个文本框,但是我在这里遇到了一个问题。 当我在键入内容后按回车键或没有在第一个文本框中键入任何内容时,它将转到下一个文本框。 但是,如果我不小心输入了某些内容并按退格键删除了第一个文本框内容,并且如果我按回车键,那么它不会移动到下一个文本框,请帮助如何解决此问题。
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);
}
}
Note:此问题总结整理于: Go to next text box when enter is pressed