C#制作登陸和注冊界面并帶有美化窗口
當(dāng)前位置:點(diǎn)晴教程→知識管理交流
→『 技術(shù)文檔交流 』
1,首先設(shè)計(jì)登錄界面,共有三個(gè),如下:
上圖登錄及注冊為linklabel控件,其他為label控件;
上圖為登陸界面,兩個(gè)textbox文本輸入框,注冊為linklabel控件; 界面設(shè)計(jì)很簡單,不說了。
2,代碼介紹: 1) 主界面(Form1):
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { this.Hide(); Form3 f3 = new Form3(); f3.ShowDialog(); }//顯示注冊界面; private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { this.Hide(); Form2 f = new Form2(); f.ShowDialog(); if (f.DialogResult == DialogResult.OK) { this.Visible = true; } }//顯示登錄界面; private void Form1_FormClosing(object sender, FormClosingEventArgs e) { try { System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcesses(); foreach (System.Diagnostics.Process myProcess in myProcesses) { if ("LoginInterface.exe" == myProcess.ProcessName) myProcess.Kill(); } } catch (Exception ee) {
MessageBox.Show(ee.Message); } }//關(guān)掉程序; 2) 注冊界面(Form3) 本文使用的數(shù)據(jù)庫是sql sever2005,先在引用里加入: using System.Data.SqlClient;
以下為程序代碼: public partial class Form3 : Form { public Form3() { InitializeComponent(); } bool flagRegister;//定義標(biāo)志位,確認(rèn)用戶注冊 string strConnect = "Data Source=CAI-PC\\SQLEXPRESS;Initial Catalog=MyData1;Persist Security Info=True;User ID=sa;Password=******"; //連接數(shù)據(jù)庫字符串 private void button1_Click(object sender, EventArgs e) { if ((textBox1.Text.Length >= 4) && (textBox1.Text.Length <= 12) && (textBox2.Text.Length >= 6) && (textBox3.Text.Length >= 6)) { flagRegister = true; } else { if ((textBox1.Text.Length < 4) || (textBox1.Text.Length > 12)) { MessageBox.Show("用戶名長度不在約定范圍內(nèi),請重新輸入!", "提示"); return; } if (textBox2.Text.Length < 6) { MessageBox.Show("密碼長度不足6位,請重新輸入!","提示"); return; } if (textBox3.Text.Length < 6) { MessageBox.Show("請重新輸入郵箱!", "提示"); return; } }//判斷用戶名條件;
if (UserFlag == true) { MessageBox.Show("用戶已經(jīng)存在,請重新輸入!"); return; }
if (flagRegister == true) //確認(rèn)用戶注冊后,把用戶寫入數(shù)據(jù)庫 { SqlConnection conConnection = new SqlConnection(strConnect); conConnection.Open(); string cmd = "insert into 用戶(用戶名,密碼,email) values (''" + textBox1.Text + "''," +"''" + textBox2.Text + "''," + "''" + textBox3.Text + "'') "; SqlCommand com = new SqlCommand(cmd, conConnection); com.ExecuteNonQuery(); conConnection.Close(); MessageBox.Show("注冊成功!點(diǎn)擊確定,返回登錄界面。", "提示"); this.Close(); Form1 f1 = new Form1(); f1.label2.Text = "歡迎你," + textBox1.Text; f1.label1.Visible = false; f1.label3.Visible = false; f1.linkLabel1.Visible = false; f1.linkLabel2.Visible = false; f1.label2.Visible = true; f1.Show(); }
}
public bool UserFlag; //定義標(biāo)志位,來確認(rèn)用戶是否存在 private void textBox1_TextChanged(object sender, EventArgs e) { SqlConnection conConnection = new SqlConnection(strConnect); conConnection.Open(); string cmd = "select 用戶名 from 用戶"; SqlCommand com = new SqlCommand(cmd, conConnection); SqlDataReader readerUser = com.ExecuteReader(); while (readerUser.Read()) { if (textBox1.Text == readerUser["用戶名"].ToString().Trim()) { label5.Text = "用戶已存在,請重新輸入!"; UserFlag = true; //textBox1.Text = ""; return; } else if (textBox1.Text != readerUser["用戶名"].ToString().Trim()) { label5.Text = "恭喜你,該用戶名可以使用。"; UserFlag = false; } } }//判斷用戶名是否滿足條件
private void textBox3_TextChanged(object sender, EventArgs e) { int index = textBox3.Text.IndexOf("@"); if (index < 1) { label7.Text = "郵箱格式不正確,請重新輸入!"; } else { label7.Text = "郵箱格式正確"; } }//判斷郵箱格式是否正確 }
3) 登錄界面(Form2) 本文使用的數(shù)據(jù)庫是sql sever2005,先在引用里加入: using System.Data.SqlClient;
以下為程序代碼: string User, Pwd; //用戶名,密碼 bool flagshow = false;//用來標(biāo)注登錄名是否存在于數(shù)據(jù)庫 private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { this.Hide(); Form3 f3 = new Form3(); f3.ShowDialog(); }//顯示注冊界面
private void button1_Click(object sender, EventArgs e) //登錄 { string strConnect = "Data Source=CAI-PC\\SQLEXPRESS;Initial Catalog=MyData1;Persist Security Info=True;User ID=sa;Password=******"; SqlConnection conConnection = new SqlConnection(strConnect); conConnection.Open(); string cmd = "select 用戶名,密碼,email from 用戶"; SqlCommand com = new SqlCommand(cmd, conConnection); SqlDataReader reader = com.ExecuteReader(); while (reader.Read())//從數(shù)據(jù)庫讀取用戶信息 { User = reader["用戶名"].ToString(); Pwd = reader["密碼"].ToString(); if (User.Trim () == textBox1.Text & Pwd.Trim () == textBox2.Text) { flagshow = true; //用戶名存在于數(shù)據(jù)庫,則為true } } reader.Close(); conConnection.Close();
if (flagshow == true) { showMainForm();//用戶存在,返回登錄界面 } else { MessageBox.Show("用戶不存在或密碼錯(cuò)誤!", "提示"); return; }
}
private void showMainForm()//登錄成功,顯示主界面 { this.Close(); Form1 f1 = new Form1(); f1.label1.Visible = false; f1.label3.Visible = false; f1.linkLabel1.Visible = false; f1.linkLabel2.Visible = false; f1.label2.Visible = true; f1.label2.Text = "歡迎你," + textBox1 .Text ; f1.Show(); }
3,為美化窗體,可下載winform皮膚包,下載地址為: http://down.51cto.com/data/139603
把皮膚文件和IrisSkin2.dll放在bin文件夾下debug文件夾中,把IrisSkin2.dll直接拖進(jìn)工具箱,即可使用。在Form1加入皮膚控件skinEngine1,Form1的構(gòu)造函數(shù)里加入 skinEngine1.SkinFile =皮膚文件路徑; 本文為: skinEngine1.SkinFile = "皮膚\\wave\\WaveColor1.ssk"; 就可以得到比較炫的窗體了。本文窗體效果如下: 主界面: 注冊界面:
注冊成功后返回登錄界面:
登錄界面:
登錄成功: 該文章在 2016/12/27 22:24:52 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |