C#中表單的一些簡單運用

來源:互聯網
上載者:User

標籤:ar   使用   sp   for   on   檔案   資料   div   art   

從今天開始,我們進入到學window form的知識,今天簡單的學習了一些控制項和事件的運用。沒有什麼很全面的理論,所以今天就總結下所寫的程式。一個簡單的註冊頁面程式

 註冊頁面程式 要求: 1:修改所有的控制項Name 屬性        2: 登入事件   檢測各個控制項是否為空白,如果是空  彈出註冊失敗    如果成功  則顯示新表單 並且 新表單上面顯示    “XXX你好! 歡迎來到雲和學院學習Net” 走馬燈形式                    密碼輸入三次那麼登入按鈕不可用  3分鐘之後可用                    把註冊資訊的各個資料按照     Rocky|admin|[email protected]|18301412747|男|足球,籃球,排球”寫入到一個文字檔中   代碼: public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }                 int num = 1;   //定義num是為了擷取輸入錯誤的次數         private void btnregster_Click(object sender, EventArgs e)        {             //如果達到三次則註冊按鈕將不能使用            if (num == 3)            {                this.btnregster.Enabled = false;            }            //定義字串來接收文本資料            string user = this.txtname.Text.Trim();            string pwd = this.txtpwd.Text.Trim();              string email = this.txtemail.Text.Trim();            string phone = this.txtphone.Text.Trim();      //判斷使用者名稱、密碼、郵箱、手機、性別、愛好是否為空白,如果為空白,則提示註冊失敗,否則則提示註冊成功,進入下一個介面                if (string.IsNullOrEmpty(user))                {                    MessageBox.Show("註冊失敗,未輸入使用者名稱!");                    ++num; //計時器的累加                }                 else if (string.IsNullOrEmpty(pwd))                {                    MessageBox.Show("註冊失敗,未輸入密碼!");                    ++num;                }                else if (txtaginpwd.Text != pwd)                {                    MessageBox.Show("註冊失敗,確認密碼必須保持一致");                    ++num;                }                 else if (string.IsNullOrEmpty(email))                {                    MessageBox.Show("註冊失敗,未輸入郵箱");                    ++num;                }                 else if (string.IsNullOrEmpty(phone))                {                    MessageBox.Show("註冊失敗,未輸入手機號");                    ++num;                }                 else if (cbkbasketball.Checked==false && cbkpaiqiu.Checked==false && cbkscore.Checked==false)//只有在都沒有被選中的情況下才顯示註冊失敗                {                    MessageBox.Show("註冊失敗,請選擇愛好!");                    ++num;                }                 else if (radman.Checked==false && radwomen.Checked==false  )                {                    MessageBox.Show("註冊失敗,請選擇性別");                      ++num;                }                else              {                   MessageBox.Show("註冊成功");                   Form2 fm = new Form2(user);//開啟Form2的表單,這裡傳入一個參數user。                   fm.Show();                   this.Hide();    //隱藏Form1的表單               }            //建立一個Regster文字文件,並寫入註冊資訊,且以分隔字元(|)隔開                string gender = string.Empty;                string like = string.Empty;<br>              //判斷性別被選中的是哪個,就擷取哪個的文本                if (radman.Checked == true)                {                    gender = radman.Text;                }                else                {                    gender = radwomen.Text;                }              //判斷愛好哪幾個被選中,則擷取選中的文本                                 if (this.cbkbasketball.Checked)                {                    like += cbkbasketball.Text + ",";                                    }                if (this.cbkpaiqiu.Checked)                {                    like += cbkpaiqiu.Text+",";                                   }                if (this.cbkscore.Checked)                {                    like += cbkscore.Text+",";                }                string[] array = { txtname.Text, txtpwd.Text, txtemail.Text, txtphone.Text, gender,like };//定義一個數組來接收註冊資訊的資料                string strs = string.Empty;                foreach (var item in array)               {                    strs += item;                    strs = string.Join("|",array);//註冊資訊在文字文件中以分隔字元隔開           }                File.WriteAllText("Regster.txt", strs);//若唯寫文檔名字,則預設的路徑是在本項目的bin目錄下。         }             private void btnconsole_Click(object sender, EventArgs e)//取消按鈕       {                              txtname.Focus();//讓使用者名稱重新擷取焦點            txtname.Text = "";            txtpwd.Text = "";            txtaginpwd.Text = "";            txtemail.Text = "";            txtphone.Text = "";            radman.Checked = false;            radwomen.Checked = false;            cbkbasketball.Checked = false;            cbkpaiqiu.Checked = false;            cbkscore.Checked = false;                     }         private void timer1_Tick(object sender, EventArgs e)         {             //輸入三次錯誤後,計時器停止輸入3分鐘後再重新輸入                this.btnregster.Enabled = true;                       }         private void Form1_Activated(object sender, EventArgs e)        {            txtname.Focus();//首先讓使用者名稱文字框獲得焦點     }              

C#中表單的一些簡單運用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.