这是一个基于C# winform窗体应用的登陆界面,包含账号密码的输入,输入错误次数的检验,以及超过三次输入错误后程序自动退出,登录跳转页面的实现,账号密码正确后的进度条登录效果
(ps:做项目的时候正好碰到了,顺手记录一下)
最终效果:
***过程***
1、登陆界面的设计,这部分比较简单,我就不再详细说明了,大概设计成下图的这个效果就可以了(所需:textbox*2,label*2,button*1,picturebox*1,panel*2,timer*1)
注意:别忘了放上timer控件!
2、再新建一个主界面用于演示跳转界面的效果(Main.cs):
**ps:这里放上一个label用来代替主界面的实际内容
****需要修改参数的控件(没提到的就不用改):
*panel:
改改两个panel的颜色就行
e.g:
*密码部分的textbox:
*picturebox:
2、代码部分:
(注意:我这里修改了一些控件的名字,在使用代码的时候,应当替换成你自己的实际控件的名字)
(1)页面跳转效果:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Digital_tanks
{
internal static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new login());
login login = new login();
if (login.ShowDialog() == DialogResult.OK)
Application.Run(new Main());
else
Application.Exit();
}
}
}
(2)登录界面的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Digital_tanks
{
public partial class login : Form
{
private bool buttonClicked = false;//初始化登录按钮状态
private int errorCount = 0;//初始化错误次数
public login()
{
InitializeComponent();
textBoxPassword.KeyDown += TextBoxPassword_KeyDown; // 绑定KeyDown事件处理程序
}
private void TextBoxPassword_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter) // 判断按下的键是否为回车键
{
button1_Click(sender, e); // 调用button1_Click方法
}
}
private void timerProgressbar_Tick(object sender, EventArgs e)
{
if (buttonClicked)
{
panelProgressbar.Width += 5;//每次计时溢出,进度条前进五
if (panelProgressbar.Width > 980)
{
timerProgressbar.Stop();//计时器停止
this.DialogResult = DialogResult.OK;//进度条跑到头时,对话框设为OK状态
}
}
}
private void button1_Click(object sender, EventArgs e)
{
String st1 = textBoxUsername.Text.Trim();
String st2 = textBoxPassword.Text.Trim();
if (st1 == "admin" && st2 == "123456789")
{
MessageBox.Show("账户、密码正确,欢迎!");
buttonClicked = true;
}
else
{
errorCount++;
if (errorCount >= 3)
{
MessageBox.Show("错误次数超过三次,程序将退出!");
Application.Exit();
}
else
{
MessageBox.Show("账户或密码错误,请重新输入!\n错误次数:" + errorCount);
textBoxUsername.Text = "";
textBoxPassword.Text = "";
}
}
}
}
}
ps:整篇文章写的比较赶,有什么错漏的地方,还请各位指出,方便我进行后续修改