vb.net 如何编写 If 语句来检查 VB 文本框中是否存在文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16618419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:37:48  来源:igfitidea点击:

How do I write an If statement to check for the existence of text in a VB textbox?

vb.netif-statement

提问by user2395588

I want a statement that will check for text in a label/textbox. For example:

我想要一个语句来检查标签/文本框中的文本。例如:

If TextBox1.Text = True then Label1.Show

OR

或者

If Label1.Text = False then Label1.Visible = False

I want my label to hide itself when there's no text input into it. I have it to where my next from TextBox1is input as the text of Label1, but I want Label1to hide when there is no text put in the textbox.

我希望我的标签在没有文本输入时隐藏自己。我把它TextBox1作为 的文本输入到我的下一个位置Label1,但是Label1当文本框中没有文本时我想隐藏。

I've tried multiple methods but none seem to work. Any ideas?

我尝试了多种方法,但似乎都不起作用。有任何想法吗?

回答by Douglas Barbin

This checks for empty, null, or whitespace-only, so there is no need to Trim(). If you don't like it on one line, you can put it inside an If statement.

这会检查空、空或仅空白,因此不需要 Trim()。如果你不喜欢在一行中,你可以把它放在一个 If 语句中。

Label1.Visible = String.IsNullOrWhiteSpace(Label1.Text)

回答by Yuriy Galanter

If you simple need to check for whether textbox/label has value, the construct is

如果您只需要检查文本框/标签是否具有值,则构造为

if label1.text.Trim() = ""

or

或者

if label1.text.Trim() = String.Empty

UpdateTo show/hide control based on existance of the text you can use oneliner:

更新要根据文本的存在显示/隐藏控件,您可以使用 oneliner:

Label1.Visible = (Label1.Text.Trim() <> "")