注册登录界面
Form1:登录界面

Form1:登录界面代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
namespace 注册登录界面
{
public partial class Form1 : Form
{
public List<string> accountList = new List<string>();//账号
public List<string> passwordList = new List<string>();//密码
public int loginFailCount = 0;//登录次数
public Form1()
{
InitializeComponent();
LoadUserData();// 加载本地用户数据
UserManager.MainForm = this;
}
//存储txt文件
//定义一个文件路径
private const string UserDataPath = "E:/桌面/111.txt";
private void LoadUserData()
{
//判断文件是否存在
if (!File.Exists(UserDataPath))
{
return;
}
//遍历数据
foreach (string line in File.ReadAllLines(UserDataPath))
{
//跳过null和""
if (string.IsNullOrEmpty(line))
{
continue;
}
//','分隔符
string[] userInfo = line.Split(',');
//判断是否分为2段
if (userInfo.Length == 2)
{
//将账号和密码分别存入对应的数组
accountList.Add(userInfo[0].Trim());
passwordList.Add(userInfo[1].Trim());
}
}
}
//登录按键
private void button1_Click(object sender, EventArgs e)
{
//先判断账号和密码是否输入内容
if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))
{
MessageBox.Show("账号和密码不能为空");
return;
}
//获取输入的登录账号
string inputAccount = textBox1.Text.Trim();
//获取输入的登录密码
string inputPassword = textBox2.Text.Trim();
//判断是否登录成功
bool isAccountExist = false;
//判断是否账号是否正确
bool isLoginSuccess = false;
//存储账号在数组中的索引
int accountIndex = -1;
//遍历账号数组,查找输入的账号是否存在
for (int i = 0; i < accountList.Count; i++)
{
//比较输入的账号和数组中的账号
if (accountList[i] == inputAccount)
{
isAccountExist = true;// 标记账号存在
accountIndex = i; // 记录该账号在数组中的索引(用于后续匹配密码)
break; // 找到账号后退出循环,无需继续遍历
}
}
// 账号不存在处理
if (!isAccountExist)
{
MessageBox.Show("账号不存在");
textBox1.Clear();// 清除密码信息
textBox1.Focus();// 聚焦
return;
}
//验证密码
if (passwordList[accountIndex] == inputPassword)
{
isLoginSuccess = true;//标记密码正确,登录成功
}
else
{
MessageBox.Show("密码错误");
textBox2.Clear();// 清除密码信息
textBox2.Focus();// 聚焦
}
//判断登录次数
if (loginFailCount >= 3)
{
MessageBox.Show("登录次数过多,请去注册");
//登录按钮失效
button1.Enabled = false;
//启动计时器
this.timer1.Start();
return;
}
//判断登录是否成功
if (isLoginSuccess)
{
MessageBox.Show("登陆成功");
loginFailCount = 0;//重置失败次数计数器
this.Close(); // 关闭登录窗体
}
else
{
loginFailCount++;
MessageBox.Show($"登录失败,剩余尝试次数:{3 - loginFailCount}");
}
}
//注册
private void button2_Click(object sender, EventArgs e)
{
// 创建注册窗体Form2的实例,传入当前的账号数组和密码数组
Form2 registerForm = new Form2(accountList, passwordList);
registerForm.Show(); // 显示注册窗体
this.Hide(); // 隐藏当前登录窗体(不关闭,注册完成后可返回)
}
//定时器事件
private void timer1_Tick(object sender, EventArgs e)
{
//登录按钮启用
this.button1.Enabled = true;
//关闭计时器
this.timer1.Stop();
loginFailCount = 0;
}
}
public static class UserManager
{
// 定义一个属性 专门接收Form1窗口对象
public static Form1 MainForm { get; set; }
}
}
Form2:注册界面

Form2:注册界面代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace 注册登录界面
{
public partial class Form2 : Form
{
//私有账号数组,接收从Form1传入的已注册账号(仅本类可用)
private readonly List<string> _accountList;
//私有密码数组,接收从Form1传入的已注册密码(仅本类可用)
private readonly List<string> _passwordList;
//静态文件路径与Form1共用同一个TXT数据文件(存储账号密码)
private const string UserDataPath = "E:/桌面/111.txt";
public Form2(List<string> accountList, List<string> passwordList)
{
InitializeComponent();
_accountList = accountList; //接收Form1传入的已注册账号数组
_passwordList = passwordList; //接收Form1传入的已注册密码数组
}
//form2注册
private void button2_Click(object sender, EventArgs e)
{
string registerAccount = textBox1.Text.Trim();//注册账号去除前后空格
string registerPassword = textBox2.Text.Trim();
//注册密码
//判断账号和密码是否输入内容
if (string.IsNullOrEmpty(registerAccount) || string.IsNullOrEmpty(registerPassword))
{
MessageBox.Show("用户名和密码不能为空!");
return;
}
//判断用户名是否重复
if (_accountList.Contains(registerAccount))
{
MessageBox.Show("用户名已存在,请重新输入!");
textBox1.Focus();
return;
}
//判断密码格式
// 密码必须包含有数字和字母,不能低于6位
string passwordRegex = "^(?=.*[a-zA-Z])(?=.*\\d)[a-zA-Z0-9]{6,}$";
// Regex 对应的有一个方法IsMatch()
if (!Regex.IsMatch(registerPassword, passwordRegex))
{
// 说明密码值不符合规则要求
MessageBox.Show("密码必须包含有数字和字母,不能低于6位");
textBox2.Clear();
textBox2.Focus();//输入焦点
return;
}
try
{
//将新账号密码写入本地TXT文件
using (StreamWriter writer = File.AppendText(UserDataPath))
{
writer.WriteLine($"{registerAccount},{registerPassword}");
}
//将新账号密码添加到内存数组
_accountList.Add(registerAccount);
_passwordList.Add(registerPassword);
MessageBox.Show("注册成功!请返回登录界面登录。");
this.Close();
UserManager.MainForm?.Show();
}
catch (Exception ex)
{
MessageBox.Show($"注册失败:{ex.Message}");
}
}
//form2登录
private void button1_Click(object sender, EventArgs e)
{
this.Close();
UserManager.MainForm?.Show();
}
}
}