C# WinForms 登录与主窗体开发:功能实现及开发陷阱规避
using System; using System.Windows.Forms; namespace LoginApp { public partial class LoginForm : Form { private int attemptCount = 0; private const int maxAttempts = 3; public LoginForm() { InitializeComponent(); } private void btnLogin_Click(object sender, EventArgs e) { string username = txtUsername.Text; string password = txtPassword.Text; if (username == "admin" && password == "password") { MainForm mainForm = new MainForm(); mainForm.Show(); this.Hide(); } else { attemptCount++; MessageBox.Show($"用户名或密码错误,剩余尝试次数: {maxAttempts - attemptCount}"); if (attemptCount >= maxAttempts) { MessageBox.Show("尝试次数过多,程序退出"); Application.Exit(); } } } private void btnExit_Click(object sender, EventArgs e) { Application.Exit(); } } public partial class MainForm : Form { public MainForm() { InitializeComponent(); } private void fileToolStripMenuItem_Click(object sender, EventArgs e) { MessageBox.Show("文件菜单被点击"); } private void editToolStripMenuItem_Click(object sender, EventArgs e) { MessageBox.Show("编辑菜单被点击"); } } } // LoginForm.Designer.cs 部分代码(需要放在单独文件中) /* namespace LoginApp { partial class LoginForm { private System.ComponentModel.IContainer components = null; private TextBox txtUsername; private TextBox txtPassword; private Button btnLogin; private Button btnExit; private Label label1; private Label label2; protected override void Dispose(bool disposing) { if (disposing && (components != null)) components.Dispose(); base.Dispose(disposing); } private void InitializeComponent() { this.txtUsername = new TextBox(); this.txtPassword = new TextBox(); this.btnLogin = new Button(); this.btnExit = new Button(); this.label1 = new Label(); this.label2 = new Label(); this.SuspendLayout(); // label1 this.label1.Location = new System.Drawing.Point(30, 30); this.label1.Text = "用户名:"; // txtUsername this.txtUsername.Location = new System.Drawing.Point(100, 30); this.txtUsername.Width = 150; // label2 this.label2.Location = new System.Drawing.Point(30, 70); this.label2.Text = "密码:"; // txtPassword this.txtPassword.Location = new System.Drawing.Point(100, 70); this.txtPassword.PasswordChar = '*'; this.txtPassword.Width = 150; // btnLogin this.btnLogin.Location = new System.Drawing.Point(70, 120); this.btnLogin.Text = "登录"; this.btnLogin.Click += new EventHandler(this.btnLogin_Click); // btnExit this.btnExit.Location = new System.Drawing.Point(170, 120); this.btnExit.Text = "退出"; this.btnExit.Click += new EventHandler(this.btnExit_Click); // LoginForm this.ClientSize = new System.Drawing.Size(300, 180); this.Controls.AddRange(new Control[] { this.label1, this.txtUsername, this.label2, this.txtPassword, this.btnLogin, this.btnExit }); this.Text = "登录窗口"; this.ResumeLayout(false); } } } // MainForm.Designer.cs 部分代码(需要放在单独文件中) /* namespace LoginApp { partial class MainForm { private System.ComponentModel.IContainer components = null; private MenuStrip menuStrip1; private ToolStripMenuItem fileToolStripMenuItem; private ToolStripMenuItem editToolStripMenuItem; protected override void Dispose(bool disposing) { if (disposing && (components != null)) components.Dispose(); base.Dispose(disposing); } private void InitializeComponent() { this.menuStrip1 = new MenuStrip(); this.fileToolStripMenuItem = new ToolStripMenuItem(); this.editToolStripMenuItem = new ToolStripMenuItem(); // fileToolStripMenuItem this.fileToolStripMenuItem.Text = "文件"; // editToolStripMenuItem this.editToolStripMenuItem.Text = "编辑"; // menuStrip1 this.menuStrip1.Items.AddRange(new ToolStripItem[] { this.fileToolStripMenuItem, this.editToolStripMenuItem }); // MainForm this.ClientSize = new System.Drawing.Size(800, 450); this.Controls.Add(this.menuStrip1); this.MainMenuStrip = this.menuStrip1; this.Text = "主窗口"; // 注册事件 this.fileToolStripMenuItem.Click += new EventHandler(this.fileToolStripMenuItem_Click); this.editToolStripMenuItem.Click += new EventHandler(this.editToolStripMenuItem_Click); } } } */ // Program.cs 入口文件 /* using System; using System.Windows.Forms; namespace LoginApp { static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new LoginForm()); } } } */ 我在以往的开发过程中,碰到过以下几点需要格外小心处理的地方: 密码存储应使用哈希加密而非明文比较 UI线程中执行长时间操作会导致界面冻结 资源释放不当可能引起内存泄漏 ######[AI写代码神器 | 1208点数解答 | 2025-09-13 14:48:14]
- Python 实现简易密码设置系统:多种功能交互与安全要点揭秘(字节豆包 | 286点数解答 | 2025-03-28 10:43:21)367
- C语言实现多功能密码设置系统:含代码及安全注意要点(字节豆包 | 414点数解答 | 2025-03-28 10:43:55)355
- C 语言实现:超简单密码设置系统,含代码与注意要点!(字节豆包 | 519点数解答 | 2025-03-28 12:42:02)393
- C语言实现密码管理系统:含功能代码与安全注意事项(字节豆包 | 409点数解答 | 2025-04-05 18:09:56)339
- C语言实现多功能密码系统:初始化、确认、设置、显示、重置与退出(字节豆包 | 454点数解答 | 2025-04-05 18:48:42)339
- C语言实现密码管理系统:含初始化、确认、设置、显示、重置及退出功能(字节豆包 | 413点数解答 | 2025-04-06 15:37:16)285
- C 语言实现密码管理系统:含初始化、确认、设置、显示、重置及退出功能(字节豆包 | 403点数解答 | 2025-04-06 15:37:54)300
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)352
- 51 单片机:定时器 0 实现 8 个 LED 循环点亮,附代码及优化建议(字节豆包 | 1193点数解答 | 2024-12-27 15:10:29)323
- Java JFrame 实现多界面跳转:含登录、注册及个人信息管理功能(讯飞星火 | 503点数解答 | 2024-06-06 23:49:29)400
- Python实现密码锁:含界面与功能,超详细代码解析!(GPT | 2535点数解答 | 2024-12-05 20:48:02)168
- Vue 3 实现浅蓝色登录页面:精美布局与实用功能全解析(字节豆包 | 333点数解答 | 2025-03-07 08:31:30)212