我写了下面的代码,除了按Enter键仍然不会单击他的Login按钮,它们都可以正常工作。帮帮我!C#按Enter键按下按钮
int failedAttempts;
private void uiPasswordTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
uiLoginButton.PerformClick();
}
}
private void uiLoginButton_Click(object sender, EventArgs e)
{
if (uiPasswordTextBox.Text == "alacaZam")
{
uiPasswordTextBox.Text = string.Empty;
this.uiLogOffButton.Enabled = true;
this.uiMarkEntryNameTextBox.Enabled = true;
this.uiMarkEntryClassTestTextBox.Enabled = true;
this.uiMarkEntryConicalTextBox.Enabled = true;
this.uiMarkEntryDestructiveTextBox.Enabled = true;
this.uiStoreMarksButton.Enabled = true;
this.uiShowMarksAZButton.Enabled = true;
this.uiShowMarks100_0Button.Enabled = true;
}
else if (failedAttempts <= 1)
{
failedAttempts++;
MessageBox.Show("You are permitted 3 attempts. " + (3 - failedAttempts) + " attempt/s remaining.", "Incorrect Password", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else if (failedAttempts == 2)
{
MessageBox.Show("Access Denied", "Incorrect Password. Entered 3 Times", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
Application.Exit();
}
}
尝试在输入上调用uiLoginButton_Click(null,null)按 –
检查此问题。 http://stackoverflow.com/questions/299086/c-sharp-how-do-i-click-a-button-by-hitting-enter-whilst-textbox-has-focus –
你应该期待设置*默认操作*(和取消操作)在您的表单上,而不是手动执行*。 –