winform项目中通常需要登录窗体来打开主窗体的功能,实现逻辑如下:
程序启动→打开登录窗体→按下登录按钮→关闭登录窗体→打开主窗体。
实现代码如下:
Program.cs中代码:
static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Form1 form1 = new Form1(); if (form1.ShowDialog() == DialogResult.OK) { Application.Run(new Form2()); } } }
登录窗体中代码如下:
public partial class FrmLogin : Form { public FrmLogin() { InitializeComponent(); } private void btnLogin_Click(object sender, EventArgs e) { if (txtUserName.Text.Trim() == "admin" && txtPassword.Text.Trim() == "111") this.DialogResult = DialogResult.OK; else MessageBox.Show("用户名或密码错误!"); } private void btnCancel_Click(object sender, EventArgs e) { Application.Exit(); } }
标签:逻辑,登录,void,DialogResult,Application,Program,窗体 From: https://www.cnblogs.com/hanzq/p/17023757.html