这俩天做登陆界面设计,也在网上查了一些资料,发现大部分都是针对某个功能介绍,而很少有完整的案列。我呢就结合自己的需求,把有些功能整合在一起了,欢迎大家修改完善。

SQL数据库设计:

登陆界面设计:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace ZH_controls
{public partial class Login : Form{public Login(){InitializeComponent();}private void Login_Load(object sender, EventArgs e){}private void Login_Button_Click(object sender, EventArgs e) //单击登陆按钮{String username, password;username = UserName.Text;password = Password.Text;String myconn = "Data Source=PC-20180112FRNM;Initial Catalog=属性表;User ID=sa;Password=123;Integrated Security=True";//数据库实例连接字符串SqlConnection sqlConnection = new SqlConnection(myconn);//新建数据库连接实例sqlConnection.Open();//打开数据库连接password = Adduser.GetMD5(password);   //在同一个命名空间(在同一个文件夹中),可以访问Adduser里的GetMD5函数。 因为MD5加密算法不可逆,所以要把输入的密码加密和数据库里密码匹配。这样做以后,除了用户自己谁也不知道密码了。String sql = "select UserName,Password from User_info where UserName='" + username + "'and Password='" + password + "'";//SQL语句实现表数据的读取SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();if (UserName.Text == ""){MessageBox.Show("请输入用户名", "登陆失败");UserName.Focus();}else if (Password.Text == ""){MessageBox.Show("请输入密码", "登陆失败");}else{if (sqlDataReader.HasRows)//满足用户名与密码一致,进入下一个界面{//实现页面跳转Form1 form3 = new Form1();this.Hide();     //隐藏当前窗体   form3.ShowDialog();Application.ExitThread();   //退出当前窗体,这一步很重要,否则最后可能无法将所有进程关闭。最好是在跳转页面后,将之前的页面退出。}else//如果登录失败,询问是否注册新用户{DialogResult dr = MessageBox.Show("是否注册新用户?", "登录失败", MessageBoxButtons.YesNo, MessageBoxIcon.Question);if (dr == DialogResult.Yes)//打开注册界面{Adduser form2 = new Adduser();this.Hide();form2.ShowDialog();Application.ExitThread();  }else{UserName.Text = "";Password.Text = "";UserName.Focus();this.Show();}}}sqlConnection.Close();}private void Register_Click(object sender, EventArgs e)     //单击注册按钮{Adduser form2 = new Adduser();this.Hide();form2.ShowDialog();Application.ExitThread();  }private void Quit_Click(object sender, EventArgs e)    //单击退出按钮{Application.Exit(); }private void UserName_KeyPress(object sender, KeyPressEventArgs e)  //功能:输入用户名后,按 Enter键,光标到输入密码的TextBox中{if (e.KeyChar == (char)Keys.Enter){Password.Focus();     //控制光标指向哪的}}private void Password_KeyPress(object sender, KeyPressEventArgs e)  //KeyPress事件,在控件具有焦点并且用户按下并释放某个键后发生{if (e.KeyChar == (char)Keys.Enter){// Login_Button.Focus();Login_Button_Click(sender ,e);}}}
}

注册页面设计:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Security.Cryptography;  //MD5密码加密  namespace ZH_controls
{public partial class Adduser : Form{public Adduser(){InitializeComponent();}private void button1_Click(object sender, EventArgs e)  //单击确定按钮{String username, password, repassword;username = textBox1.Text;password = textBox2.Text;repassword = textBox3.Text;if (textBox1.Text == ""){ MessageBox.Show("请输入用户名", "注册失败");textBox1.Focus();}else if (textBox2.Text == ""){MessageBox.Show("请输入密码", "注册失败");textBox2.Focus();}else if (textBox3.Text == "")MessageBox.Show("请确认密码", "注册失败");else{string myConn = "Data Source=PC-20180112FRNM;Initial Catalog=属性表;User ID=sa;Password=123;Integrated Security=True";SqlConnection sqlConnection = new SqlConnection(myConn);    //实例化连接对象  sqlConnection.Open();String sql = "select UserName from User_info where UserName='" + username + "'";//SQL语句实现表数据的读取SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();if (sqlDataReader.HasRows){sqlConnection.Close();MessageBox.Show("该用户名已存在,请重新注册", "注册失败");textBox1.Text = "";textBox2.Text = "";textBox3.Text = "";textBox1.Focus();     //指定光标在哪个textBox处闪烁}else{if (password == repassword)//两次输入的密码一致  {sqlConnection.Close();string myConn2 = "Data Source=PC-20180112FRNM;Initial Catalog=属性表;User ID=sa;Password=123;Integrated Security=True";SqlConnection sqlConnection2 = new SqlConnection(myConn2);   //实例化连接对象  sqlConnection.Open();password = GetMD5(password);String sql2 = "INSERT INTO User_info(UserName,Password) VALUES('" + username + "','" + password + "')";//SQL语句向表中写入数据  SqlCommand sqlCommand2 = new SqlCommand(sql2, sqlConnection);sqlCommand2.ExecuteNonQuery();sqlConnection2.Close();DialogResult dr = MessageBox.Show("是否返回主界面", "注册成功", MessageBoxButtons.YesNo, MessageBoxIcon.Question);if (dr == DialogResult.Yes)//打开注册界面{Login form2 = new Login();this.Hide();form2.ShowDialog();Application.ExitThread();}else{textBox1.Text = "";textBox2.Text = "";textBox3.Text = "";this.Show();}}else{MessageBox.Show("两次输入密码不一致", "错误信息");textBox1.Text = "";textBox2.Text = "";textBox3.Text = "";}}}            }private void button2_Click(object sender, EventArgs e) //返回登陆按钮{Login  form3 = new Login();this.Hide();form3.ShowDialog();Application.ExitThread();   }public static string GetMD5(String input)   //MD5算法,输入一段字符串,输出一段字符串{string cl = input;string pwd = "";MD5 md5 = MD5.Create();//实例化一个md5对像// 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择 byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得for (int i = 0; i < s.Length; i++){// 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符 pwd = pwd + s[i].ToString("X");}return pwd;}private void textBox1_KeyPress(object sender, KeyPressEventArgs e)  {if (e.KeyChar == (char)Keys.Enter){textBox2.Focus();}}private void textBox2_KeyPress(object sender, KeyPressEventArgs e){if (e.KeyChar == (char)Keys.Enter){textBox3.Focus();}}private void textBox3_KeyPress(object sender, KeyPressEventArgs e){if (e.KeyChar == (char)Keys.Enter){button1_Click(null, null);}}}
}

C#+winform登陆界面案例相关推荐

  1. winform登陆功能案例

    文章目录 前言 一.问题描述 二.解题思路 1.登录界面设计 2.数据库 3.登录 三.功能实现 1.链接数据库 2.验证登录信息功能的函数 3.实现验证码功能 写一个类用于创建验证码 更新验证码的方 ...

  2. PyQt5 开发注册、登陆和功能界面案例与知识点梳理

    最近在学习 PyQt5 GUI 编程,大致路线是找了套网课<撩课-Python-GUI编程-PyQt5>,以梳理思维导图的形式梳理了下基础知识点以及 QtDesigner 应用流程,跳过各 ...

  3. C# WinForm窗体制作以图片为背景的登陆界面

    一.Form窗体 1.标题栏不显示 FormBorderStyle = None; 2.任务栏不显示 ShowInTaskbar = false; 3.关闭按钮不显示 ControlBox = fal ...

  4. Winform UI界面设计例程(一)多窗口主题搭配

    本专栏会有二十多篇关于winform UI界面设计的案例讲解,不使用任何第三方美化库,完全纯代码编写,写出的界面可以达到WPF界面的效果 本篇实现的效果如下图所示,不同界面自动切换主题配色,子界面嵌入 ...

  5. Winform UI界面设计例程(二)主题风格切换

    本专栏会有二十多篇关于winform UI界面设计的案例讲解,包括自定义控件的美化等,完全不使用任何第三方美化库,纯代码编写,写出的界面可以达到WPF界面的效果 接上一篇 上一篇我们把左侧按钮区组态好 ...

  6. 【登录异常解决】Ubuntu 输入正确的密码后重新返回到登陆界面

    [登录异常解决]Ubuntu 输入正确的密码后重新返回到登陆界面 参考文章: (1)[登录异常解决]Ubuntu 输入正确的密码后重新返回到登陆界面 (2)https://www.cnblogs.co ...

  7. 描述linux系统从开机到登陆界面的启动过程

    简述: 1.开机BIOS自检 2.MBR引导 3.grub引导菜单 4.加载内核kernel 5.启动init进程 6.读取inittab文件,执行rc.sysinit,rc等脚本 7.启动minge ...

  8. QML与C++交互:登陆界面设计

    环境: 主机:WIN7 开发环境:Qt5.2.1 说明: QML设计前台界面,C++后台负责逻辑 效果图: 源代码: 前台qml文件 login.qml [javascript] view plain ...

  9. 怎么显示全部背景图片_Windows 聚焦图片在锁屏界面和登陆界面没有显示

    一. Windows 聚焦图片在锁屏界面和登陆界面没有显示 首先请确定设置中聚焦功能是否已经打开.打开设置-个性化-锁屏界面,确定一下背景下拉框选项是否已经设置为Windows聚焦.同时设置" ...

最新文章

  1. 开发顺序工作流时注意的几个事项
  2. win7台式电脑怎么连wifi_台式电脑怎么用wifi网络
  3. Word2Vec学习笔记(五)——Negative Sampling 模型(续)
  4. lamuda表达式 list移除空元素_Lambda 表达式遍历集合时用remove方法删除list集合中满足条件的元素问题...
  5. linux常用命令笔记大全
  6. 推特大规模攻击幕后黑手竟是 17 岁少年?企业云安全迫在眉睫!
  7. CF995C Leaving the Bar
  8. 小米8刷官方欧版rom并从国内版rom提取安装MiPay、门卡模拟
  9. 泰克示波器存储格式,在存储时怎么选择?
  10. php草莓派,甜品控的减脂早餐,这样做低卡又裹腹,草莓派香蕉派,好吃分享了...
  11. JAVA面大厂简历怎么写_大厂程序员来谈一谈要如何写简历!!(附简历模板)...
  12. 无向有权图的邻接矩阵实现(C++)
  13. Windows中Redis的下载安装与修改密码并启动
  14. RGB565部分常用颜色对照表
  15. 弘扬企业家精神!闪马智能创始人兼CEO彭垚再获殊荣
  16. 51单片机的电子密码锁的设计与仿真
  17. 【Matlab学习笔记】报错——检查对函数‘Link’的调用中是否缺失参数或参数数据类型不正确。
  18. 【Linux 主机ssh远程连接暴力破解详解】
  19. 深度解析粉象生活VS花生日记哪个更好,哪个更有优势更容易赚钱
  20. JAVA简单模拟商品购买,记录购买日志

热门文章

  1. 【笔记】wlan - 基础概念(无线、wifi、常见协议、频谱、信道、ap部署、案例)
  2. 教学|怎样制作360度全景图,更炫更酷3D建模步骤
  3. SE 软件工程期末总结
  4. Django+模板引擎+Bootstrap +sqlite3 个人博客管理系统(附开源代码)
  5. 如何学习Python技术?自学Python需要多久?
  6. 07数据库设计(概念结构、逻辑结构)
  7. android显示通知图标大全,Android应用开发之android 桌面APP应用图标显示通知消息的数量显示与去除...
  8. AndroidStudio-断点调试-也许你该知道断点调试是有多么的美好
  9. 氢燃料电池发动机性能测试
  10. 基因编辑相关最新研究进展(2022年12月)