標籤:參數 按鈕 gpo 結果 gre 修改 具體步驟 應用程式 use
C#基礎入門 十Windows應用程式的介面設計
private void Cancel_Click(object sender, EventArgs e) { DialogResult result = MessageBox.Show("確定要取消嗎?", "操作提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { this.Close(); } }
- 使用者輸入驗證:修改上述例子,單擊“確定”按鈕,彈出訊息框,顯示登陸訊息提示。具體步驟:
- 給“確定”按鈕添加Click事件;
- 處理“確定”按鈕的單擊事件,彈出訊息框,如果沒有輸入使用者名稱或密碼,彈出相應的訊息框給出提示,如果已經輸入使用者名稱和密碼,則彈出訊息框確定使用者是否登入。
private void OK_Click(object sender, EventArgs e) { //判斷輸入的使用者名稱是否為空白 if (this.userName.Text.Trim().Equals(string.Empty)) { MessageBox.Show("請輸入使用者名稱!"); this.userName.Focus();//是該文字框獲得焦點 } //判斷輸入的密碼是否為空白 else if (this.Pwd.Text.Trim().Equals(string.Empty)) { MessageBox.Show("請輸入密碼!"); this.Pwd.Focus(); } else { DialogResult result = MessageBox.Show("確定要登入嗎?","登入提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Information); if (result == DialogResult.OK) { MessageBox.Show("登入成功!"); } } }
- 運行結果如所示:(圖21、22、23)
控制項進階
- 選項類控制項:選項按鈕為使用者提供兩個或多個呼哧選項組成的選項集,使用者在一組選項按鈕中只能選擇一個。
- 選項按鈕的應用--話費儲值:選擇儲值的金額並輸入手機號碼,單擊“儲值”按鈕,顯示儲值訊息提示,介面設計如所示(圖24):
private void btnRecharge_Click(object sender, EventArgs e) { int money = 0; if (phonename.Text == "") { MessageBox.Show("請輸入儲值號碼!"); } else if (phonename.TextLength < 11) { MessageBox.Show("非手機號碼不能儲值!"); } else { if (Rdl10.Checked == true) { money = 10; } else if (Rdl30.Checked == true) { money = 30; } else if (Rdl50.Checked == true) { money = 50; } MessageBox.Show("儲值成功!儲值號碼:"+phonename.Text+",儲值金額"+money); } }
C#基礎入門 十