1,现在也是好久没有做winfrom程序了,有点时间,写点小东西记录下,
见过最多的还是先启动登录窗体,然后成功后,将其隐藏,当然可以,但是有更好的方式,
代码如下:
2,登录窗体代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Login : Form { public Login() { InitializeComponent(); } //登录 private void button_login_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.OK; this.Close(); } //取消 private void btn_Cancel_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; this.Close(); } } }2,主窗体代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Main : Form { private bool UserLogin() { Login frmLogin = new Login(); try { DialogResult result = frmLogin.ShowDialog(); if (result == DialogResult.OK) { return true; } else { return false; } } finally { frmLogin.Dispose(); } } public Main() { if (!UserLogin()) { //登录被取消退出系统,这一句很重要 System.Environment.Exit(0); //Application.Exit(); } InitializeComponent(); } } }重点在于,弹出登录页面为对话框的形式,然后登陆成功与否可以有结果返回到主窗体判断。比较简单,记录下!
欢迎交流!