目录
功能概述
实现思路
一、程序入口(Program.cs)
二、登录用户控件(Login.cs)
2.1 控件初始化与密码显示逻辑
2.2 登录控件设计器(Login.Designer.cs)
三、主窗体(Form1.cs)
3.1 初始化与数据加载
3.2 登录功能实现
3.3 数据持久化(文件操作)
3.4 记录管理(删除功能+鼠标右击选中)
3.5 主窗体设计器(Form1.Designer.cs)
四、模块间关系说明
设计器代码说明
功能亮点
在日常开发中,我们经常需要记录用户的登录信息以便后续查看和管理。今天我将分享一个基于 WinForm 的简单用户登录记录器实现,该程序能够记录用户登录时间和账号信息,并支持对记录进行管理。
功能概述
这个登录记录器主要包含以下功能:
- 用户登录界面(账号、密码输入)
- 登录记录显示(右侧列表)
- 登录记录持久化存储(保存到本地文件)
- 记录管理(右键菜单删除功能)
- 鼠标右键选中功能(鼠标右键Mouse UP事件)
- 登录时间记录(DateTime)
- 复合控件的使用
实现思路
- 使用 WinForm 构建主界面和登录控件
- 通过 JSON 格式将登录记录保存到本地文件
- 实现登录记录的增删查功能
- 提供右键菜单用于删除记录
一、程序入口(Program.cs)
这是应用程序的启动点,负责初始化和启动主窗体:
using System;
using System.Windows.Forms;namespace UMP
{internal static class Program{/// <summary>/// 应用程序的主入口点/// </summary>[STAThread] // 标记为单线程单元,WinForm 必须使用此特性static void Main(){// 启用视觉样式,使界面更美观Application.EnableVisualStyles();// 设置文本渲染方式为默认Application.SetCompatibleTextRenderingDefault(false);// 启动主窗体Application.Run(new Form1());}}
}
核心作用:作为程序的启动器,初始化应用程序并加载主窗体。
二、登录用户控件(Login.cs)
这是一个自定义用户控件,封装了登录相关的界面元素和功能:
2.1 控件初始化与密码显示逻辑
using System.Windows.Forms;namespace UMP
{public partial class Login : UserControl{// 定义委托用于跨控件通信public delegate void AddAccountToForm(string accountInfo);public event AddAccountToForm OnAccountAdded;public Login(){InitializeComponent();// 初始化密码框,用*隐藏密码textBox2.PasswordChar = '*';}// 密码显示/隐藏切换private void checkBox1_CheckedChanged(object sender, EventArgs e){// 复选框选中时显示明文,否则显示*textBox2.PasswordChar = checkBox1.Checked ? '\0' : '*';}}
}
核心作用:
- 提供账号密码输入界面
- 实现密码显示 / 隐藏功能
- 定义事件委托用于与主窗体通信
2.2 登录控件设计器(Login.Designer.cs)
设计器代码定义了登录控件的界面元素:
namespace UMP
{partial class Login{private System.ComponentModel.IContainer components = null;protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}private void InitializeComponent(){// 初始化各种控件this.label1 = new System.Windows.Forms.Label();this.textBox1 = new System.Windows.Forms.TextBox(); // 账号输入框this.textBox2 = new System.Windows.Forms.TextBox(); // 密码输入框this.label2 = new System.Windows.Forms.Label();this.label3 = new System.Windows.Forms.Label(); // "用户登录"标题this.checkBox1 = new System.Windows.Forms.CheckBox(); // 显示密码复选框this.button1 = new System.Windows.Forms.Button(); // 登录按钮// 省略控件属性设置代码...// 绑定事件this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);}// 定义控件变量,供外部访问public System.Windows.Forms.TextBox textBox1;public System.Windows.Forms.TextBox textBox2;public System.Windows.Forms.Button button1;}
}
核心作用:通过代码定义了登录界面的所有控件及其布局、样式和事件绑定。
三、主窗体(Form1.cs)
主窗体是程序的主界面,负责协调各部分功能:
3.1 初始化与数据加载
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Windows.Forms;namespace UMP
{public partial class Form1 : Form{// 定义数据存储路径(当前目录下的"账号.txt")private string FilePath => Path.Combine(Directory.GetCurrentDirectory(), "账号.txt");public Form1(){InitializeComponent();// 绑定登录按钮的点击事件login1.button1.Click += Button1_Click;// 加载已保存的登录记录try{using (StreamReader sr = new StreamReader(FilePath)){string data = sr.ReadToEnd();// 解析JSON数据JArray jsondata = JsonConvert.DeserializeObject<JArray>(data);if (jsondata != null){// 将记录添加到列表框foreach (string jobj in jsondata){listBox1.Items.Add(jobj);} }}}catch (FileNotFoundException){// 文件不存在时不做处理(首次运行时正常)}catch (Exception ex){MessageBox.Show($"加载数据出错: {ex.Message}");}}}
}
核心作用:
- 定义数据存储路径
- 初始化界面并绑定事件
- 程序启动时加载历史登录记录
3.2 登录功能实现
// 登录按钮点击事件处理
private void Button1_Click(object sender, EventArgs e)
{// 验证账号密码不为空if (login1.textBox1.Text != "" && login1.textBox2.Text != ""){// 获取当前时间DateTime time = DateTime.Now;// 添加登录记录到列表listBox1.Items.Add($"登录时间:{time} 登录账号:{login1.textBox1.Text}");// 保存记录到文件Writedata();}else{// 提示用户输入账号密码MessageBox.Show("请输入账号和密码!");}
}
核心作用:
- 验证用户输入
- 记录登录信息(时间 + 账号)
- 触发数据保存操作
3.3 数据持久化(文件操作)
// 将登录记录保存到文件
public void Writedata()
{try{// 将列表框中的数据序列化为JSON格式string Jsondata = JsonConvert.SerializeObject(listBox1.Items, Formatting.Indented);// 写入文件using (StreamWriter sw = new StreamWriter(FilePath, false, Encoding.UTF8)){sw.WriteLine(Jsondata);}}catch (Exception ex){MessageBox.Show($"保存数据出错: {ex.Message}");}
}
核心作用:使用 JSON 格式将登录记录持久化到本地文件,保证程序重启后数据不丢失。
3.4 记录管理(删除功能+鼠标右击选中)
#region 删除int index;private void 删除账号ToolStripMenuItem_Click(object sender, EventArgs e){listBox1.Items.RemoveAt(index); // 从列表移除Writedata();}//鼠标右键选择private void listBox1_MouseUp(object sender, MouseEventArgs e){if (e.Button == MouseButtons.Right){//选中当前点击位置的数据 的索引index = listBox1.IndexFromPoint(e.Location);//将当前点击位置的数据 选中listBox1.SelectedIndex = index;//判断但当前是否选中数据if (index >= 0){//选中点击的项listBox1.SetSelected(index, true);删除账号ToolStripMenuItem.Enabled = true;}else{//listBox1.SelectedIndex = -1;listBox1.ClearSelected();删除账号ToolStripMenuItem.Enabled = false;}}}#endregion
核心作用:实现登录记录的删除功能,包括右键菜单交互和文件数据更新。
3.5 主窗体设计器(Form1.Designer.cs)
定义主窗体的布局和控件:
namespace UMP
{partial class Form1{private System.ComponentModel.IContainer components = null;protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}private void InitializeComponent(){this.components = new System.ComponentModel.Container();this.listBox1 = new System.Windows.Forms.ListBox(); // 显示登录记录的列表this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);this.删除账号ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); // 删除菜单this.login1 = new UMP.Login(); // 登录控件// 省略控件属性设置...// 绑定事件this.listBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.listBox1_MouseUp);this.删除账号ToolStripMenuItem.Click += new System.EventHandler(this.删除账号ToolStripMenuItem_Click);}// 定义控件变量private System.Windows.Forms.ListBox listBox1;private Login login1;private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;private System.Windows.Forms.ToolStripMenuItem 删除账号ToolStripMenuItem;}
}
核心作用:定义主窗体的布局,包含左侧的登录控件和右侧的记录列表。
四、模块间关系说明
- 调用关系:Program → Form1 → Login(主窗体包含登录控件)
- 数据流向:
- 用户输入 → Login 控件 → Form1 处理 → 保存到文件
- 文件 → Form1 加载 → 显示到 listBox1
- 事件传递:Login 控件的按钮事件在 Form1 中处理,实现了控件间的解耦
通过这样的模块化设计,代码结构清晰,各部分职责明确,便于维护和扩展。
设计器代码说明
除了上述核心代码外,我们还需要设计器代码来定义界面布局。设计器代码主要包含:
- Form1.Designer.cs:定义主窗体布局,包含一个列表框(用于显示记录)和一个登录用户控件
- Login.Designer.cs:定义登录控件的布局,包含账号输入框、密码输入框、显示密码复选框和登录按钮
设计器代码由 Visual Studio 自动生成,主要定义了控件的位置、大小、字体等属性以及控件间的层次关系。
功能亮点
- 数据持久化:使用 JSON 格式将登录记录保存到本地文件,下次启动程序时自动加载
- 用户体验:密码框支持显示 / 隐藏切换,操作更友好
- 记录管理:通过右键菜单可以方便地删除不需要的记录
- 代码结构:使用用户控件分离登录功能,使代码结构更清晰