如何编写文本框的代码,不带空值 [英] How to write a code for textboxes take without null values

查看:134
本文介绍了如何编写文本框的代码,不带空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,



我有两个文本框,如TextBox1和TextBox2。现在每当点击提交按钮我想要错误更改消息如果两个文本框都是空的,如果任何一个文本框数据填充它显示成功消息。





注意:我需要使用C#代码至少一个TextBox1或TextBox2或两者的文本数据?





谢谢高级。

Dear Friends,

I have two textboxes like TextBox1 & TextBox2. Now whenever click to submit button i want error alter message if both textboxes are empty, if either one textbox data is fill it shows successfull message.


Note: I need data at least one textbox either TextBox1 or TextBox2 or both by using C# code?


Thanks in advanced.

推荐答案

这是需要考虑的事情:文本box值永远不会为null但是可以为空。



所以,对于这两个,一个非常短的行检查将是

Here is the thing to take into account: a text box value is never null but can be empty.

So, for the two, a very short line to check would be
if (TextBox1.Text + TextBox2.Text != string.Empty) //...



但是,它是可能您的要求没有非空文本,但也没有文本只包含空格字符。如果是这样,可以通过稍微复杂的内线来改进检查:


However, it's possible that your requirement is not having non-empty text, but also not having the text consisting only of the blank space characters. If so, the check could be improved with a bit more complex on-liner:

if (TextBox1.Text.Trim() + TextBox2.Text.Trim() != string.Empty) //...





一个注意事项:从未使用过汽车生成的名称,如 TextBox1 TextBox2 ;它们违反(良好)Microsoft命名约定,并非真正用于永久使用;您需要使用正确的英语拼写重命名所有此类名称以更正语义敏感的名称。它将极大地提高代码的可维护性。



-SA



One note: never ever use auto-generated names like TextBox1 or TextBox2; they violate (good) Microsoft naming conventions and are not really intended for permanent use; you need to rename all such name to correct semantically sensible names using correct English spelling. It will greatly improve maintainability of your code.

—SA


你好







在提交按钮点击活动上写下以下代码。





if(string.IsNullOrEmpty(TextBox1.Text)&& string.IsNullOrEmpty(TextBox2.Text))

{

MessageBox。显示(错误);

}

其他

{

//你在这里编码吗? />
}
Hello



Write below code on submit button click event.


if(string.IsNullOrEmpty(TextBox1.Text) && string.IsNullOrEmpty(TextBox2.Text))
{
MessageBox.Show("Error");
}
else
{
//Do you code here
}


这篇关于如何编写文本框的代码,不带空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆