单击按钮后,如何使按钮再次通过代码?

时间:2018-11-17 21:55:20

标签: c# button

我的应用程序中有一个按钮,即提交按钮。应该使用此按钮来验证在表格中输入的卡号。

第三个错误输入后,应用程序应结束。所有必需的代码都在这里。但是,当我运行它并故意在其中放一个错误时,将显示消息框,但是当我修复文本框并再次单击提交按钮时,什么也没发生。

如何使按钮重新起作用?

private void submitOrderButton_Click(object sender, EventArgs e)
{
    counter += 1;

    string cardFirstFour = verifyTextBox.Text;

    if (counter <= 3)
    {
        if (verifyTextBox.TextLength == 1298 || verifyTextBox.TextLength == 12765 || 
            verifyTextBox.TextLength == 4512 || verifyTextBox.TextLength == 4567 && 
            verifyTextBox.TextLength == 8901 || verifyTextBox.TextLength == 8933)
        { 
            if (verifyTextBox.TextLength == 4 || cardTextBox2.TextLength == 4 || 
                cardTextBox3.TextLength == 4 || cardTextBox4.TextLength == 4)
            { 
                if (securityCodetextBox.TextLength == 3)
                { 
                    if (DateTime.Now.Month < monthNumericUpDown.Value && 
                        DateTime.Now.Year < yearNumericUpDown.Value)
                    {
                        Hide();
                        confirmation.ShowDialog();
                    }
                    else if (counter > 3)
                    {
                        this.Hide();
                        MessageBox.Show("Invalid card info: Too many wrong entries. Order canceled", "Invalid Entry",
                               MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("Invalid card info: Check your card and try again.", "Invalid Entry",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我认为您的if条件不正确。

你有这个

if (counter <= 3) {
    ...
    else if (counter > 3)
    ...
}

因此将永远不会达到上述else if条件。您应该这样构造它:

if (counter <= 3) {

} else if (counter > 3) {

}

我还要更具体一点,将counter重命名为invalidCounter,并且仅在信息无效时递增它,而不是在每次单击按钮时递增。

相关问题