酷代码 AI
菜单
服务商

编写WinForms 应用程序,1制作登录窗体,包含登录、退出按钮,用户名文本框、密码输入框。2设计主窗体,包含MenuStrip菜单项,并注册菜单项事件。 点击登录,进行用户名、密码验证,如果输入错误3次,退出应用程序,如果登录成功显示主窗体。点击退出,退出应用程序。 语言方向:C# 系统环境:Windows

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线程中执行长时间操作会导致界面冻结 资源释放不当可能引起内存泄漏 [2025-09-13 14:48:14 | AI写代码神器 | 1208点数解答]

相关提问
发财导航,免费问AI
实用工具查看更多