ok怎么登陆(WinForm(四)一种实现登录的方式)
2023-04-26 富美财经 浏览量:次
首先声明,这只是一种登录方式,并不是最好的方式,用这个例子为了说明登录窗体和Application的关系。
在登录前,定义了用户实体,然后是一个通用的类,存放进程中当前登录的用户,所以CurrentUser是静态类。
internal class User{ public int ID { get; set; } public string? Name { get; set; } public string? UserName { get; set; } public string? Password { get; set; }}internal class Common{ internal static User? CurrentUser { get; set; }}
这里的登录窗体不受Application管理,当登录成功后,会进入Application Run的主窗体。登录窗体要用ShowDialog模态化显示方式,让进程阻塞在登录窗体上,然后等待结束登录完成关闭后,获取登录窗体的对话窗结果,这里是如果Ok,定义为登录成功。
namespace WinFormDemo03{ internal static class Program { [STAThread] static void Main() { ApplicationConfiguration.Initialize(); var loginForm = new LoginForm(); if (loginForm.ShowDialog() == DialogResult.OK) { Application.Run(new MainForm()); } } }}
登录窗体的布局
登录按钮中要验证当前用户和密码是否存在,存在的话,就把用户保存在Common.CurrentUser中,以供后续主窗体或其他窗体使用,成功登录后要把当前窗体的DialogResult设置成Ok,因为在Main函数里,这就是判断登录的条件。
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;using WinFormDemo03.Models;namespace WinFormDemo03{ public partial class LoginForm : Form { private readonly List<User> _users; public LoginForm() { _users = new List<User>() { new User{ ID=1,Name="桂素伟", UserName="gsw",Password="abc123" }, new User{ ID=2,Name="张三", UserName="zs",Password="123abc" } }; InitializeComponent(); } private void loginButton_Click(object sender, EventArgs e) { Common.CurrentUser = _users.SingleOrDefault(s => s.UserName == usernameTextBox.Text && s.Password == passwordTextBox.Text); if (Common.CurrentUser == null) { MessageBox.Show("用户名或密码错误,请重新输入!"); } else { this.DialogResult = DialogResult.OK; } } }}
登录的实现完成了:
1、登录是模态窗体,阻塞后台操作,登录成功,继续运行,失败,通出进程。
2、登录成功后要保留登录用户,以备后用。
3、登录成功与否是用了窗体的DialogResult,当然也可以定义其他属性来完成。
本例中是说明一种思路,现实中的登录方式各种各样,有次数限制的,有与三方通信的,还有指纹人脸的,都是在最基础上作增量。希望对你有所收获。
-
-
okex微博下载欧易okex官方下载地址
欧易交易平台是最早一批成立的比特币交易平台,是现存最靠谱的平台之一,...
- 货币百科 2024-05-09
-
okex微博下载欧易okex官方下载地址
-
-
欧义ouyi官网苹果手机okxapp最新版安卓
欧易是一款专业的货币交易平台,在软件中用户朋友们可以轻松了解到数字货...
- 货币百科 2024-05-09
-
欧义ouyi官网苹果手机okxapp最新版安卓
-
-
【欧易2023】okex元宇宙下载鸥易苹果商店怎么下载OKEX
欧易是一个非常不错的货币买卖平台。平台中的买卖系统十分平安。欧易的用...
- 货币百科 2024-05-09
-
【欧易2023】okex元宇宙下载鸥易苹果商店怎么下载OKEX
-
-
ouyi交易所电脑版下载教程ouyi手机版下载官网
欧易交易所是世界主流的虚拟货币交易所app,用户们可以在这里自由交易自己...
- 货币百科 2024-05-09
-
ouyi交易所电脑版下载教程ouyi手机版下载官网
-
-
安卓哪里下载鸥易交易所鸥易交易所App官方
欧易安卓版是一个非常专业好用的数字货币交易软件,而且在软件中也是有着...
- 货币百科 2024-05-09
-
安卓哪里下载鸥易交易所鸥易交易所App官方
-
-
电子钱包ouyiapp交易平台下载_ouyi钱包APP中文版ok交易所官网下载
欧易钱包app国际版是一款非常不错的区块链交易软件,小编觉得软件设计的非...
- 货币百科 2024-05-09
-
电子钱包ouyiapp交易平台下载_ouyi钱包APP中文版ok交易所官网下载