zoukankan      html  css  js  c++  java
  • 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. }

  • 相关阅读:
    (转)创建Windows服务(Windows Services)N种方式总结
    无法加载协定为“ServiceReference1.xxxxxx”的终结点配置部分,因为找到了该协定的多个终结点配置。请按名称指示首选的终结点配置部分。
    《App架构实践指南》
    Awesome Projects (汇聚全球所有🐮项目,你值得拥有)
    【公告】个人站点及系列文章
    Android+TensorFlow+CNN+MNIST 手写数字识别实现
    TensorFlow基础
    UiAutomator2.0升级填坑记
    那些年,从博客到出书的博主
    Appuim源码剖析(Bootstrap)
  • 原文地址:https://www.cnblogs.com/grj001/p/12224906.html
Copyright © 2011-2022 走看看