使用Visual Studio写一个简单的Windows窗体应用登录界面
来源:互联网 发布:章子怡长相知乎 编辑:程序博客网 时间:2024/05/24 05:53
需要的知识:C#的基本语法,以及Visual Studio的基本操作方法
编辑软件:Sql Server 2017,Visual Studio 2017
前提:Sql Server 中有一个名为"MY-LAPTOP"的服务器,一个名为"Test"的数据库,库下有一个名为"Login_Table"的表
一、编写Form1,登录主界面
首先编写Form1.Designer.cs
namespace WindowsFormsApp3{ partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要修改 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.linkLabel1 = new System.Windows.Forms.LinkLabel(); this.checkBox1 = new System.Windows.Forms.CheckBox(); this.SuspendLayout(); // // label1 // this.label1.AutoSize = true; this.label1.Enabled = false; this.label1.Font = new System.Drawing.Font("宋体", 15F); this.label1.ForeColor = System.Drawing.SystemColors.ControlText; this.label1.Location = new System.Drawing.Point(49, 41); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(87, 25); this.label1.TabIndex = 0; this.label1.Text = "账号:"; this.label1.Click += new System.EventHandler(this.label1_Click); // // label2 // this.label2.AutoSize = true; this.label2.Enabled = false; this.label2.Font = new System.Drawing.Font("宋体", 15F); this.label2.ForeColor = System.Drawing.SystemColors.ControlText; this.label2.Location = new System.Drawing.Point(49, 98); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(87, 25); this.label2.TabIndex = 1; this.label2.Text = "密码:"; this.label2.Click += new System.EventHandler(this.label2_Click); // // button1 // this.button1.Font = new System.Drawing.Font("宋体", 13F); this.button1.Location = new System.Drawing.Point(54, 168); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(82, 33); this.button1.TabIndex = 2; this.button1.Text = "登录"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Font = new System.Drawing.Font("宋体", 13F); this.button2.Location = new System.Drawing.Point(190, 168); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(90, 33); this.button2.TabIndex = 3; this.button2.Text = "注册"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // button3 // this.button3.Font = new System.Drawing.Font("宋体", 13F); this.button3.Location = new System.Drawing.Point(329, 168); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(79, 33); this.button3.TabIndex = 4; this.button3.Text = "取消"; this.button3.UseVisualStyleBackColor = true; this.button3.Click += new System.EventHandler(this.button3_Click); // // textBox1 // this.textBox1.Font = new System.Drawing.Font("宋体", 14F); this.textBox1.Location = new System.Drawing.Point(134, 38); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(274, 34); this.textBox1.TabIndex = 5; // // textBox2 // this.textBox2.Font = new System.Drawing.Font("宋体", 14F); this.textBox2.Location = new System.Drawing.Point(134, 96); this.textBox2.Name = "textBox2"; this.textBox2.PasswordChar = '*'; this.textBox2.Size = new System.Drawing.Size(274, 34); this.textBox2.TabIndex = 6; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(54, 217); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(0, 15); this.label3.TabIndex = 7; // // linkLabel1 // this.linkLabel1.AutoSize = true; this.linkLabel1.Location = new System.Drawing.Point(321, 134); this.linkLabel1.Name = "linkLabel1"; this.linkLabel1.Size = new System.Drawing.Size(83, 15); this.linkLabel1.TabIndex = 8; this.linkLabel1.TabStop = true; this.linkLabel1.Text = "忘记密码>>"; // // checkBox1 // this.checkBox1.AutoSize = true; this.checkBox1.Location = new System.Drawing.Point(415, 110); this.checkBox1.Name = "checkBox1"; this.checkBox1.Size = new System.Drawing.Size(89, 19); this.checkBox1.TabIndex = 9; this.checkBox1.Text = "显示密码"; this.checkBox1.UseVisualStyleBackColor = true; this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(508, 244); this.Controls.Add(this.checkBox1); this.Controls.Add(this.linkLabel1); this.Controls.Add(this.label3); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Controls.Add(this.button3); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button3; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Label label3; private System.Windows.Forms.LinkLabel linkLabel1; private System.Windows.Forms.CheckBox checkBox1; }}
以下代码中编写了SqlHelp类,其作用是封装连接数据库语句,这样就可以不用每次都编写连接数据库语句,如此便可快速完成编写
如果对SqlHelp类不理解可以参考:http://blog.csdn.net/yinnan0422/article/details/51691096
如果对SqlCommand用法不理解可以参考:http://blog.csdn.net/jintougao/article/details/8587661
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data;using System.Data.Sql;using System.Data.SqlClient;using System.Windows.Forms;namespace WindowsFormsApp3{ class SqlHelp { /// <summary> /// DataSource : MY-LAPTOP /// Initial Catalog: Test /// User ID: /// Password: /// </summary> private const string connectionString = @"Data Source=MY-LAPTOP;Initial Catalog=Test;Integrated Security = SSPI"; public SqlHelp() { } public static SqlConnection conn; //打开数据库连接 public static void OpenConn() { string SqlCon = connectionString;//数据库连接字符串 conn = new SqlConnection(SqlCon); if (conn.State==ConnectionState.Closed) { conn.Open(); } } //关闭数据库连接 public static void CloseConn() { if(conn.State.ToString().ToLower() == "open") { conn.Close(); conn.Dispose(); } } //读取数据 public static SqlDataReader GetDataReaderValue(string sql) { OpenConn(); SqlCommand cmd = new SqlCommand(sql,conn); SqlDataReader dr = cmd.ExecuteReader(); CloseConn(); return dr; } //返回DataSet public static DataSet GetDataSetValue(string sql, string tableName) { OpenConn(); SqlDataAdapter da = new SqlDataAdapter(sql,conn); DataSet ds = new DataSet(); da.Fill(ds, tableName); CloseConn(); return ds; } //返回DataView public static DataView GetDataViewValue(string sql) { OpenConn(); SqlDataAdapter da; DataSet ds = new DataSet(); da = new SqlDataAdapter(sql, conn); da.Fill(ds, "temp"); CloseConn(); return ds.Tables[0].DefaultView; } //返回DataTable public static DataTable GetDataTableValue(string sql) { OpenConn(); SqlDataAdapter da = new SqlDataAdapter(sql,conn); DataTable dt = new DataTable(); da.Fill(dt); CloseConn(); return dt; } //执行一个Sql操作:添加,删除,更新操作 public void ExecuteNonQuery(string sql) { OpenConn(); SqlCommand cmd; cmd = new SqlCommand(sql, conn); cmd.ExecuteNonQuery(); cmd.Dispose(); CloseConn(); } //执行一个Sql操作:添加,删除,更新操作,返回受影响行数 public int ExecuteNonQueryCount(string sql) { OpenConn(); SqlCommand cmd; cmd = new SqlCommand(sql, conn); int value = cmd.ExecuteNonQuery(); return value; } //执行一条返回第一条记录第一列的SqlCommand命令 public object ExecuteScalar(string sql) { OpenConn(); SqlCommand cmd = new SqlCommand(sql, conn); object value = cmd.ExecuteScalar(); return value; } //返回记录数 public int SqlServerRecordCount(string sql) { OpenConn(); SqlCommand cmd = new SqlCommand(); cmd.CommandText = sql; cmd.Connection = conn; SqlDataReader dr; dr = cmd.ExecuteReader(); int RecordCount = 0; while (dr.Read()) { RecordCount++; } CloseConn(); return RecordCount; } //判断是否为数字 public static bool GetSafeValue(string value) { if (string.IsNullOrEmpty(value)) { return false; } foreach (char ch in value) { if (!char.IsDigit(ch)) { return false; } } return true; } }}
如此在Form1的监听器方法中,便可调用以上SqlHelp方法,以便响应登录按钮的触发
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 System.Data.Sql;using System.Data.SqlClient;namespace WindowsFormsApp3{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void label2_Click(object sender, EventArgs e) { } private void label1_Click(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { string accStr = this.textBox1.Text; string pswStr = this.textBox2.Text; if(accStr == "" || pswStr == "") { MessageBox.Show("用户名和密码不能为空,请重新输入"); } else { string selectSql = "select * from Login_Table where Account='" + accStr + "' and Password='"+pswStr+"'"; SqlHelp sqlHelper = new SqlHelp(); int count = sqlHelper.SqlServerRecordCount(selectSql); if (count > 0)//count大于0说明匹配成功 { MessageBox.Show("信息验证成功"); Form2 form = new Form2(); form.Show(); this.Hide(); } else { MessageBox.Show("用户名或者密码错误"); } } } private void button2_Click(object sender, EventArgs e) { //尚未更新 } private void button3_Click(object sender, EventArgs e) { Application.ExitThread(); } private void Form1_Load(object sender, EventArgs e) { //还没更新 } private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (checkBox1.Checked) { textBox2.PasswordChar = new Char();//当勾选框的时候,为显示密码 } else { textBox2.PasswordChar = '*';//当不勾选的时候,为密文状态 } } }}
二、编写主界面Form2
namespace WindowsFormsApp3{ partial class Form2 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // label1 // this.label1.AutoSize = true; this.label1.Font = new System.Drawing.Font("宋体", 15F); this.label1.Location = new System.Drawing.Point(128, 37); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(287, 25); this.label1.TabIndex = 0; this.label1.Text = "欢迎您,您已经成功登录"; // // button1 // this.button1.Font = new System.Drawing.Font("宋体", 13F); this.button1.Location = new System.Drawing.Point(133, 116); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 33); this.button1.TabIndex = 1; this.button1.Text = "确定"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Font = new System.Drawing.Font("宋体", 13F); this.button2.Location = new System.Drawing.Point(340, 116); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(75, 33); this.button2.TabIndex = 2; this.button2.Text = "退出"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // Form2 // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(550, 184); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.label1); this.Name = "Form2"; this.Text = "Form2"; this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Label label1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; }}
主界面控件完成后即可,在“登录”按钮操作下,课进入数据库中比对,如果账号密码比对成功,即可进入Form2登录主界面,否则提示错误。
阅读全文
0 0
- 使用Visual Studio写一个简单的Windows窗体应用登录界面
- java应用——写一个超简单的QQ登录界面(没有监听)
- jsp写一个简单的登录界面,有默认值
- html 写的一个登录界面
- java 的简单应用--设计一个简单的变色登录界面
- mysql+phpstrom10.04写一个简单登录注册界面
- JSP一个简单的登录界面
- 一个简单的java登录小界面
- 制作一个简单的登录界面
- Android 一个简单的登录界面
- 一个简单的用户登录界面
- JavaScript一个用户登录的简单界面
- JSP—一个简单的登录界面
- Android 一个简单的登录界面
- Android 一个简单的登录界面(一)
- java实现一个简单的登录界面
- Kotlin实现一个简单的登录界面
- Android写一个简单的欢迎界面
- ubuntu开机出现:system program problem detected
- 登录记住我功能实现
- 有序单链表的合并
- 莫比乌斯函数求法
- 蓝桥杯 基础练习 十进制转十六进制
- 使用Visual Studio写一个简单的Windows窗体应用登录界面
- 数据结构实验之二叉树七:叶子问题
- 八数码问题 BFS
- [模板]用倍增求LCA问题
- 在Linux下使用VSCode开发python3.6的环境配置及一些坑
- Spring Aop(十四)——Aop自动创建代理对象的原理
- 开源项目方式提交数据-AsyncHttpClient
- Java学习——位运算符
- 大话设计模式----外观模式