user2395588
user2395588

Reputation: 15

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

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 TextBox1 is input as the text of Label1, but I want Label1 to hide when there is no text put in the textbox.

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

Upvotes: 1

Views: 6937

Answers (2)

Douglas Barbin
Douglas Barbin

Reputation: 3615

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.

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

Upvotes: 4

suff trek
suff trek

Reputation: 39767

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

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

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

Upvotes: 2

Related Questions