C#+winform登陆界面案例

C#+winform登陆界面案例

2018年04月28日 11:27:45 朗文2048 阅读数 6834

 版权声明:本文为博主原创文章,未经博主许可不得转载 https://blog.csdn.net/langwen2048/article/details/80116665

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

    SQL数据库设计:

 

    登陆界面设计:

 
  1. using System;

  2. using System.Collections.Generic;

  3. using System.ComponentModel;

  4. using System.Data;

  5. using System.Drawing;

  6. using System.Linq;

  7. using System.Text;

  8. using System.Windows.Forms;

  9. using System.Data.SqlClient;

  10.  
  11.  
  12. namespace ZH_controls

  13. {

  14. public partial class Login : Form

  15. {

  16. public Login()

  17. {

  18. InitializeComponent();

  19. }

  20.  
  21. private void Login_Load(object sender, EventArgs e)

  22. {

  23.  
  24. }

  25.  
  26. private void Login_Button_Click(object sender, EventArgs e) //单击登陆按钮

  27. {

  28. String username, password;

  29. username = UserName.Text;

  30. password = Password.Text;

  31. String myconn = "Data Source=PC-20180112FRNM;Initial Catalog=属性表;User ID=sa;Password=123;Integrated Security=True";//数据库实例连接字符串

  32. SqlConnection sqlConnection = new SqlConnection(myconn);//新建数据库连接实例

  33. sqlConnection.Open();//打开数据库连接

  34.  
  35. password = Adduser.GetMD5(password); //在同一个命名空间(在同一个文件夹中),可以访问Adduser里的GetMD5函数。 因为MD5加密算法不可逆,所以要把输入的密码加密和数据库里密码匹配。这样做以后,除了用户自己谁也不知道密码了。

  36. String sql = "select UserName,Password from User_info where UserName='" + username + "'and Password='" + password + "'";//SQL语句实现表数据的读取

  37. SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);

  38. SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();

  39. if (UserName.Text == "")

  40. {

  41. MessageBox.Show("请输入用户名", "登陆失败");

  42. UserName.Focus();

  43. }

  44. else if (Password.Text == "")

  45. {

  46. MessageBox.Show("请输入密码", "登陆失败");

  47.  
  48. }

  49. else

  50. {

  51. if (sqlDataReader.HasRows)//满足用户名与密码一致,进入下一个界面

  52. {

  53.                     //实现页面跳转

  54. Form1 form3 = new Form1();

  55. this.Hide();     //隐藏当前窗体   

  56. form3.ShowDialog();

  57. Application.ExitThread(); //退出当前窗体,这一步很重要,否则最后可能无法将所有进程关闭。最好是在跳转页面后,将之前的页面退出。

  58. }

  59. else//如果登录失败,询问是否注册新用户

  60. {

  61. DialogResult dr = MessageBox.Show("是否注册新用户?", "登录失败", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

  62. if (dr == DialogResult.Yes)//打开注册界面

  63. {

  64.  
  65. Adduser form2 = new Adduser();

  66. this.Hide();

  67. form2.ShowDialog();

  68. Application.ExitThread();

  69. }

  70. else

  71. {

  72. UserName.Text = "";

  73. Password.Text = "";

  74. UserName.Focus();

  75. this.Show();

  76. }

  77. }

  78. }

  79. sqlConnection.Close();

  80. }

  81.  
  82. private void Register_Click(object sender, EventArgs e) //单击注册按钮

  83. {

  84.  
  85. Adduser form2 = new Adduser();

  86. this.Hide();

  87. form2.ShowDialog();

  88. Application.ExitThread();

  89. }

  90.  
  91. private void Quit_Click(object sender, EventArgs e) //单击退出按钮

  92. {

  93. Application.Exit();

  94. }

  95.  
  96.  
  97. private void UserName_KeyPress(object sender, KeyPressEventArgs e) //功能:输入用户名后,按 Enter键,光标到输入密码的TextBox中

  98. {

  99. if (e.KeyChar == (char)Keys.Enter)

  100. {

  101. Password.Focus(); //控制光标指向哪的

  102. }

  103. }

  104.  
  105. private void Password_KeyPress(object sender, KeyPressEventArgs e) //KeyPress事件,在控件具有焦点并且用户按下并释放某个键后发生

  106. {

  107. if (e.KeyChar == (char)Keys.Enter)

  108. {

  109. // Login_Button.Focus();

  110. Login_Button_Click(sender ,e);

  111. }

  112. }

  113.  
  114. }

  115. }

注册页面设计:

 
  1. using System;

  2. using System.Collections.Generic;

  3. using System.ComponentModel;

  4. using System.Data;

  5. using System.Drawing;

  6. using System.Linq;

  7. using System.Text;

  8. using System.Windows.Forms;

  9. using System.Data.SqlClient;

  10. using System.Security.Cryptography; //MD5密码加密

  11.  
  12. namespace ZH_controls

  13. {

  14. public partial class Adduser : Form

  15. {

  16. public Adduser()

  17. {

  18. InitializeComponent();

  19. }

  20.  
  21. private void button1_Click(object sender, EventArgs e) //单击确定按钮

  22. {

  23. String username, password, repassword;

  24. username = textBox1.Text;

  25. password = textBox2.Text;

  26. repassword = textBox3.Text;

  27. if (textBox1.Text == "")

  28. {

  29. MessageBox.Show("请输入用户名", "注册失败");

  30. textBox1.Focus();

  31. }

  32. else if (textBox2.Text == "")

  33. {

  34. MessageBox.Show("请输入密码", "注册失败");

  35. textBox2.Focus();

  36. }

  37. else if (textBox3.Text == "")

  38. MessageBox.Show("请确认密码", "注册失败");

  39. else

  40. {

  41. string myConn = "Data Source=PC-20180112FRNM;Initial Catalog=属性表;User ID=sa;Password=123;Integrated Security=True";

  42. SqlConnection sqlConnection = new SqlConnection(myConn); //实例化连接对象

  43. sqlConnection.Open();

  44. String sql = "select UserName from User_info where UserName='" + username + "'";//SQL语句实现表数据的读取

  45. SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);

  46. SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();

  47.  
  48. if (sqlDataReader.HasRows)

  49. {

  50. sqlConnection.Close();

  51. MessageBox.Show("该用户名已存在,请重新注册", "注册失败");

  52. textBox1.Text = "";

  53. textBox2.Text = "";

  54. textBox3.Text = "";

  55. textBox1.Focus(); //指定光标在哪个textBox处闪烁

  56. }

  57. else

  58. {

  59. if (password == repassword)//两次输入的密码一致

  60. {

  61. sqlConnection.Close();

  62. string myConn2 = "Data Source=PC-20180112FRNM;Initial Catalog=属性表;User ID=sa;Password=123;Integrated Security=True";

  63. SqlConnection sqlConnection2 = new SqlConnection(myConn2); //实例化连接对象

  64. sqlConnection.Open();

  65.  
  66. password = GetMD5(password);

  67. String sql2 = "INSERT INTO User_info(UserName,Password) VALUES('" + username + "','" + password + "')";//SQL语句向表中写入数据

  68. SqlCommand sqlCommand2 = new SqlCommand(sql2, sqlConnection);

  69. sqlCommand2.ExecuteNonQuery();

  70. sqlConnection2.Close();

  71. DialogResult dr = MessageBox.Show("是否返回主界面", "注册成功", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

  72. if (dr == DialogResult.Yes)//打开注册界面

  73. {

  74.  
  75. Login form2 = new Login();

  76. this.Hide();

  77. form2.ShowDialog();

  78. Application.ExitThread();

  79. }

  80. else

  81. {

  82. textBox1.Text = "";

  83. textBox2.Text = "";

  84. textBox3.Text = "";

  85. this.Show();

  86. }

  87.  
  88. }

  89. else

  90. {

  91. MessageBox.Show("两次输入密码不一致", "错误信息");

  92. textBox1.Text = "";

  93. textBox2.Text = "";

  94. textBox3.Text = "";

  95. }

  96. }

  97. }

  98. }

  99.  
  100. private void button2_Click(object sender, EventArgs e) //返回登陆按钮

  101. {

  102. Login form3 = new Login();

  103. this.Hide();

  104. form3.ShowDialog();

  105. Application.ExitThread();

  106.  
  107. }

  108.  
  109. public static string GetMD5(String input) //MD5算法,输入一段字符串,输出一段字符串

  110. {

  111. string cl = input;

  112. string pwd = "";

  113. MD5 md5 = MD5.Create();//实例化一个md5对像

  114. // 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择 

  115. byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));

  116. // 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得

  117. for (int i = 0; i < s.Length; i++)

  118. {

  119. // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符

  120.  
  121. pwd = pwd + s[i].ToString("X");

  122.  
  123. }

  124. return pwd;

  125. }

  126.  
  127. private void textBox1_KeyPress(object sender, KeyPressEventArgs e)

  128. {

  129. if (e.KeyChar == (char)Keys.Enter)

  130. {

  131. textBox2.Focus();

  132. }

  133. }

  134.  
  135. private void textBox2_KeyPress(object sender, KeyPressEventArgs e)

  136. {

  137. if (e.KeyChar == (char)Keys.Enter)

  138. {

  139. textBox3.Focus();

  140. }

  141. }

  142.  
  143. private void textBox3_KeyPress(object sender, KeyPressEventArgs e)

  144. {

  145. if (e.KeyChar == (char)Keys.Enter)

  146. {

  147. button1_Click(null, null);

  148. }

  149. }

  150.  
  151.  
  152.  
  153. }

  154. }

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
WinForms是一种用于创建Windows桌面应用程序的编程框架。它是在微软的Windows操作系统上使用的标准.NET框架的一部分。使用WinForms,开发人员可以通过拖放控件并在代码中编写事件处理程序来快速创建具有丰富用户界面的桌面应用程序。 WinForms提供了一系列预定义的控件,如文本框,按钮,标签等,以及容器控件,如面板,表格和选项卡控件。开发人员可以通过设置这些控件的属性和使用事件处理程序来控制其行为和外观。通过编写代码,可以在控件之间建立交互,并使应用程序响应用户的操作。 使用WinForms,开发人员可以访问和操作Windows操作系统的原生功能和特性。例如,可以使用WinForms API来操作文件和文件夹,读写注册表,显示消息框和对话框等。此外,WinForms还提供了对绘图和图形处理的支持,使开发人员能够创建自定义的图形界面和图像。 与其他桌面应用程序开发框架相比,WinForms具有易学易用的特点。它使用C#编程语言,并提供了丰富的文档和教程,使开发人员能够快速上手。此外,WinForms还具有良好的可扩展性和兼容性,可以与其他.NET技术,如ASP.NET和WPF等进行集成和交互。 总的来说,WinForms是一种强大而方便的桌面应用程序开发框架,它使开发人员能够快速创建功能丰富的Windows桌面应用程序并与操作系统进行交互。无论是入门级开发人员还是有经验的专业人士,都可以通过学习和使用WinForms来构建出色的桌面应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值