且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#中的空文本框验证

更新时间:2023-12-06 12:11:52

假设你的文本框名称ID是 txtBox 。然后在你的提交按钮代码中尝试这个:



Lets say you textbox name ID is txtBox. then try this in your submit button code:

if(txtBox.Text.Trim() == string.Empty)
{
 Messagebox.Show("Please enter something in the textbox");
 return; // return because we don't want to run normal code of buton click
}

// your normal button code will some here


试试这个,

try this out,
if (TextBox1.Text == "")
                    MessageBox.Show("Enter Some Text in TextBox1");


对于空文本框验证我们需要遵循以下代码:

For Empty textbox validation we need to follow the below code:
if (textBox1.Text == "")
{
   string myStringVariable1 = string.Empty;
   MessageBox.Show("User Name is requierd");
}
else if (textBox2.Text == "")
{
   string myStringVariable2 = string.Empty;
   MessageBox.Show("Password is requierd");
}
//For combobox validation we should follow the below code.
else if (comboBox1.Text == "--Choose--")
{
   string myStringVariable3 = string.Empty;
   MessageBox.Show("Select User Type");
}