提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
#(一)创建登录界面
##1.第一种
##2.第二种
#(二)登录界面的加载
前言
提示:这里可以添加本文要记录的大概内容:
用作记录 ,温故知新。。
提示:以下是本篇文章正文内容,下面案例可供参考
一、登录界面是什么?
登录界面是软件框架的基础部分。
二、使用步骤
1.引入库
代码如下(示例):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp14
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Application.Run(new login());
login login = new login();
login.ShowDialog();
if (login.DialogResult==DialogResult.OK)
{
login.Dispose();
Application.Run(new MainForm());
}
else if (login.DialogResult==DialogResult.Cancel)
{
login.Dispose();
return;
}
}
}
}
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 WindowsFormsApp14
{
public partial class login : Form
{
public login()
{
InitializeComponent();
}
private void butLogin_Click(object sender, EventArgs e)
{
string userName = this.textBox1.Text;
string userPassword = this.textBox2.Text;
if (userName.Equals("")||userPassword.Equals(""))
{
MessageBox.Show("密码不能为空!");
}
else
{
if (userName.Equals("admin")&&userPassword.Equals("121212"))
{
MessageBox.Show("登录成功");
this.DialogResult = DialogResult.OK;
this.Dispose();
this.Close();
}
else
{
MessageBox.Show("用户名或密码错误");
}
}
}
}
}
界面数量,控件名称
总结
平台Vs2017,语言C#,据传此种方法可能造成频闪;
第二种办法
/*在C#中从登陆界面进入主界面,进入主界面以后怎么关闭登陆界面
有很多方法,我就说两种
方法一:
在登录界面的登录按钮的单击事件下,写代码,这种情况其实把主界面看作登录界面的子窗体。只是把登录界面隐藏,它还存在于内存中,不过一般登录界面很小,不占多少资源,也无所谓。*/
private void btnLogin_Click(object sender, EventArgs e)
{
if (checkpw())//验证密码是否正确的函数
{
FormMain fmain = new FormMain();//new主窗体
this.Hide();//隐藏登录窗体
fmain.Show();//显示主窗体
}
else
{
MessageBox.Show("用户名或密码错误!",);
}
}
//方法二:
//在主窗体Load事件下写如下代码,
private void FormMain_Load(object sender, EventArgs e)
{
FormLogin flogin = new FormLogin();
flogin.ShowDialog();
}
//在子窗体登录按钮单击事件下写如下代码
private void btnLogin_Click(object sender, EventArgs e)
{
if (checkpw())//验证密码是否正确的函数
{
this.Close();//关闭登录界面
}
else
{
MessageBox.Show("用户名或密码错误!",);
}
}
//这种情况其实把登录界面看作为主界面的子窗体,登陆界面关闭后,释放内存。