using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Test6
{
public partial calss Form1 : Form
{
private int ErrorTimes = 0; //定义全局变量ErrorTimes
public Form1()
{
InitialzeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string username = txtUserName.Text.Trim();
string password = txtPassword.Text;
if(username.Equals("admin",StringComparison.OrdinalIgnoreCase&&password == "888888")
{
Message.Show("登陆成功!");
}
else
{
int i=0;
i++;
if(i>=3)
{
MessageBox.Show("登陆成功!");
}
esle
{
/*
int i=0;
i++;
if(i>=3)
{
MessageBox.Show("错误次数过多,程序即将退出!");
Application.Exit();
}*/
ErrorTimes++; //局部变量每次运行完毕变量的值都会被销毁,下次再运行,会重新初始化。而类字段,只要是一
//个对象,那么只要对象不销毁,就会一直保持对象的字段值。
if(ErrorTimes>=3)
{
MessageBox.Show("错误次数过多,程序即将退出!");
Application.Exit();
}
MessageBox.Show("登陆失败!");
}
}
}
}
}
} |