![](/img/trans.png)
[英]Validate empty TextBox, NumericUpdown and ComboBox with generic code in C#
[英]c# code to prevent registering with textbox empty
當某些或所有文本框為空時,如何防止注冊?
private void btnRegister_Click(object sender, EventArgs e)
{
UserRegistration UR = new UserRegistration(); //
int x = UR.SaveDetails(
txtFirstname.Text,
txtLastname.Text,
txtUsername.Text,
txtPassword.Text,
cboCourse.Text,
cboLevel.Text,
txtRegistrationAmount.Text,
txtRegistrationId.Text,
txtDateOfRegistration.Text);
MessageBox.Show(x.ToString());
if (txtFirstname.Text.Trim() = "")
MessageBox.Show("YOU MUST ENTER ALL FIELDS!")
}
有很多方法可以驗證您的TextBox。 您可以在以后的每次擊鍵或Validating事件中執行此操作。
如果您的TextBox失去焦點,則會觸發Validating事件。 例如,當用戶單擊另一個控件時。 如果您設置的e.Cancel = true,則TextBox不會失去焦點。 希望這可以幫助 :)
樣品:
private void textBox1_Validating(object sender, CancelEventArgs e)
{
if (textBox1.Text != "something")
e.Cancel = true;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.