C#中的空文本框验证 [英] Empty Textbox Validation in C#
本文介绍了C#中的空文本框验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是c#全新的。所以我只是进入了基础知识。现在我开始使用Windows窗体。现在我必须添加2个文本框和1个插入按钮现在,当我进入文本框时,它应该在单击按钮时插入。现在我需要的是当文本框为空时,当我点击按钮时,它应该显示错误。我认为这是非常小的问题。但是因为我是新手,所以我会指导我这样做。
问候
Balamurugan
Hi,
I am completely new to c# .so i am just into basics.Just now i am beginning to work with windows forms.Now i have to add a 2 text boxes and 1 insert button now when i'm entering the textboxes it should be inserted when the button is clicked.Now what i need is that when the text boxes is empty and when i clicked the button , it should show a error.I Think this is very small problem. But as i'm new, pl guide me to do it .
Regards
Balamurugan
推荐答案
假设你的文本框名称ID是txtBox
。然后在你的提交
按钮代码中尝试这个:
Lets say you textbox name ID istxtBox
. then try this in yoursubmit
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");
}
这篇关于C#中的空文本框验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文