CS中实现简单的注册验证窗体程序
生活随笔
收集整理的這篇文章主要介紹了
CS中实现简单的注册验证窗体程序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
場景
效果
?
實現
打開VS,新建窗體程序
?
快捷鍵ctrl+alt+x打開工具箱,拖拽label、TextBox、Button,實現注冊窗體布局。
在輸入框TextBox后面還有對應的提示用的label,將其Text設為空,設置好name屬性,將字體顏色設為紅色。
?
將密碼以及確認密碼的TextBox的屬性--行為--PasswordChar設置為*
在解決資源管理器窗口下右擊項目--添加--類
User實體類
?
User代碼
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace RegisterTest {class User{private string name;public string Name{get { return name; }set { name = value; }}private string pwd;public string Pwd{get { return pwd; }set { pwd = value; }}private string email;public string Email{get { return email; }set { email = value; }}} }同理添加Validate驗證類
?
回到頁面設計雙擊注冊按鈕進入其點擊事件的代碼編寫中
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;namespace RegisterTest {public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){Validate validate = new Validate();bool result = false;User user = new User();user.Name = this.username.Text.Trim();user.Pwd = this.password.Text.Trim();user.Email = this.email.Text.Trim();if (!validate.CheckName(user.Name)){this.nameMsg.Text = "* 用戶名無效";this.username.Text = "";}else if(!validate.Checkpwd(user.Pwd,confirmPassword.Text.Trim())){this.nameMsg.Text = "";this.pwdMsg.Text = "* 密碼無效";this.password.Text = "";this.confirmPassword.Text = "";}else if (!validate.CheckEmail(user.Email)){this.nameMsg.Text = "";this.pwdMsg.Text = "";//設置提示框的文本this.emailMsg.Text = "* 電子郵件無效";this.email.Text = "";}else{//將輸入框后面的提示框設置為空this.nameMsg .Text= "";this.pwdMsg.Text = "";this.emailMsg.Text="";result = true;}if (result){//??? \n代表換行? {0} 代表占位符 與后面參數對應string message = string.Format("用戶注冊成功! \n 用戶名:{0} \n密碼:{1}",user.Name,user.Pwd);//彈窗提示信息MessageBox.Show(message);}}private void button2_Click(object sender, EventArgs e){this.Close();}private void Form1_Load(object sender, EventArgs e){}} }效果
?
?
?
?
?
?
?
示例源碼下載
https://download.csdn.net/download/badao_liumang_qizhi/11561049
總結
以上是生活随笔為你收集整理的CS中实现简单的注册验证窗体程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#窗体中的textBox怎么设置为密码
- 下一篇: VS2013中提示:没有可放置在工具箱上