C# 当表单超过错误登录次数时,我似乎无法关闭表单

C# 当表单超过错误登录次数时,我似乎无法关闭表单,c#,visual-studio,C#,Visual Studio,当表单超过错误登录次数时,我似乎无法关闭表单 我想知道当我的错误登录尝试超过3次时关闭表单的程序 private void button1_Click(object sender, EventArgs e) { string nama = textBox1.Text; string pass = textBox2.Text; if (nama.Equals(pass) == true) {

当表单超过错误登录次数时,我似乎无法关闭表单 我想知道当我的错误登录尝试超过3次时关闭表单的程序

       private void button1_Click(object sender, EventArgs e)
    {
        
        string nama = textBox1.Text;
        string pass = textBox2.Text;


        if (nama.Equals(pass) == true)
        {
            MessageBox.Show("login success");
        }
       
        else
        {
            MessageBox.Show("login failed");
            for (int i = 1; i<=3; i++)
            {
                MessageBox.Show("login amount exceeded");
                this.Close();
            }
        }

    }
}
private void按钮1\u单击(对象发送者,事件参数e)
{
字符串nama=textBox1.Text;
字符串传递=textBox2.Text;
如果(纳米等于(通过)=真)
{
MessageBox.Show(“登录成功”);
}
其他的
{
MessageBox.Show(“登录失败”);

对于(int i=1;i您需要在
按钮1\u click
事件之外设置一个失败登录尝试计数器;否则,每次单击按钮时,您的变量都将重新初始化为0。在您的代码中,您甚至没有在变量中跟踪它,您只是有一个for循环。因此,您的代码应该如下所示:

int failedLoginAttemps = 0;


private void button1_Click(object sender, EventArgs e)
{
    
    string nama = textBox1.Text;
    string pass = textBox2.Text;

    //you can instead do this in C#: if (name == pass)
    if (nama.Equals(pass) == true)
    {
        //you probably want to reset the counter if successful
        failedLoginAttempts = 0;
        MessageBox.Show("login success");
    }
   
    else
    {
        //increment the counter here
        failedLoginAttempts ++; 

        MessageBox.Show("login failed");
        if(failedLoginAttempts >= 3)
        {
            MessageBox.Show("login amount exceeded");
            this.Close();
        }
    }

}

按钮1\u click
事件之外,您需要有一个失败登录尝试计数器;否则,每次单击按钮时,您的变量都将重新初始化为0。在您的代码中,您甚至没有在变量中跟踪它,您只是有一个for循环。因此,您的代码应该如下所示:

int failedLoginAttemps = 0;


private void button1_Click(object sender, EventArgs e)
{
    
    string nama = textBox1.Text;
    string pass = textBox2.Text;

    //you can instead do this in C#: if (name == pass)
    if (nama.Equals(pass) == true)
    {
        //you probably want to reset the counter if successful
        failedLoginAttempts = 0;
        MessageBox.Show("login success");
    }
   
    else
    {
        //increment the counter here
        failedLoginAttempts ++; 

        MessageBox.Show("login failed");
        if(failedLoginAttempts >= 3)
        {
            MessageBox.Show("login amount exceeded");
            this.Close();
        }
    }

}

也许从在表单上显示失败的登录尝试次数开始。这将为您修复代码提供一个良好的开端。是的,我可以这样做,但即使在3次尝试后程序仍会继续循环。我希望我的程序在我点击3次错误的密码尝试时关闭,然后从实际计数您的错误密码尝试开始?也许从显示e表单上失败的登录尝试次数。这将为您修复代码提供一个良好的开端。是的,我可以这样做,但即使在3次尝试后程序仍会继续循环。我希望我的程序在我输入3次错误的密码时关闭,然后开始计算您的错误密码尝试次数?