如何检查文本框是否为空? [英] how to check whether a text box is empty ?

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

问题描述

有没有人知道如何检查文本框是否为空?

does anybody have any idea about how to check whether a text box is empty ?

推荐答案

使用此代码

Use this code
if(!string.IsNullOrEmpty(tb.Text))// Note. Tb is an instance of a TextBox in your application
{   
  // Do something
}


使用 String.IsNullOrWhiteSpace [ ^ ]方法而不是String.IsNullOrEmpty()。



String.IsNullOrWhiteSpace()是<$的组合c $ c> String.IsNullOrEmpty()和 String.Trim()。长度== 0



String.Trim() [ ^ ]删除所有前导和尾随空白字符



Use String.IsNullOrWhiteSpace[^] method instead of String.IsNullOrEmpty().

String.IsNullOrWhiteSpace() is a combination of String.IsNullOrEmpty() and String.Trim().Length == 0

The String.Trim()[^] removes all leading and trailing white-space characters

if(string.IsNullOrWhiteSpace(this.textBox1.Text))
{
  MessageBox.Show("TextBox is empty");
}


嗨anyavacy,



您也可以使用此代码:

Hi anyavacy,

You can also use this code:
if(textBox1.Text.Equals(""))
{
    // write the code you want
}





我希望这有帮助,

祝你好运,

:)



I hope this helps,
Good luck,
:)


这篇关于如何检查文本框是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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