1 //限定用戶名必須為字母 2 PRivate void txtName_KeyPress(object sender, KeyPressEventArgs e) 3 { 4 if ((e.KeyChar >= 'a' && e.KeyChar <= 'z') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z')) 5 { 6 e.Handled = false; 7 } 8 else { 9 MessageBox.Show("用戶名只能為字母!");10 e.Handled = true;11 }12 }
1 //光標進入文本框時,背景為藍色,字體為白色; 2 //光標離開文本框時,背景為白色,字體為黑色。 3 private void txtName_Enter(object sender, EventArgs e) 4 { 5 txtName.ForeColor = Color.White; 6 txtName.BackColor = Color.Blue; 7 } 8 9 private void txtName_Leave(object sender, EventArgs e)10 {11 txtName.BackColor = Color.White;12 txtName.ForeColor = Color.Black;13 }
3.當輸入用戶名“admin”和密碼“123”之后,單擊”確定“按鈕,系統(tǒng)將彈出消息框以顯示輸入正確,否則顯示用戶名或密碼錯誤的提示信息。
1 private void btnLogin_Click(object sender, EventArgs e) 2 { 3 string userName = txtName.Text; 4 string passWord = txtPwd.Text; 5 if (userName == "admin" && password == "123") 6 { 7 MessageBox.Show("歡迎進入個人理帳系統(tǒng)!", "登陸成功!", MessageBoxButtons.OK, MessageBoxIcon.Information); 8 } 9 else10 {11 MessageBox.Show("您輸入的用戶名或密碼錯誤!", "登錄失敗!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);12 }13 }
4.單擊”取消“按鈕,清除輸入信息,并將光標定位在txtName文本框中。
1 private void btnCancel_Click(object sender, EventArgs e)2 {3 txtName.Text = "";4 txtPwd.Text = "";5 txtName.Focus();6 }
Notice:
新聞熱點
疑難解答