Share via


How to check whether a TextBox Has any Text in vb

Question

Saturday, September 28, 2013 7:50 AM

How to check whether a TextBox Has any Text in vb 

Visual Studio MSDN

All replies (8)

Sunday, September 29, 2013 3:29 AM ✅Answered | 1 vote

How to check whether a TextBox Has any Text in vb 

Visual Studio MSDN

You can do that in so many ways but you don't mention what method you desire to perform the test with. This code uses the mouse hovering over the TextBox to begin the test.

Private Sub TextBox1_MouseHover(sender As Object, e As EventArgs) Handles TextBox1.MouseHover
        If TextBox1.TextLength = 0 Then
            MessageBox.Show("TextBox is empty")
        End If
End Sub

By the way Cor, congratulations on going over 100,000. That's awesome.

Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.


Saturday, September 28, 2013 7:53 AM

if(TextBox1.Text!="")
{
   …………
}

Click For donating:Free Rice For the poor
For spamming-sender issues, you can either report it at Microsoft Spamming Issue, or just find "Report Spam Here+Number" at Forum Issue;You can also find "Verify Your Account+Number" at "Forum Issue", where you can submit to be confirmed to paste links or images.
For more things to talk about? StackOverFlow is your choice.


Saturday, September 28, 2013 9:27 AM | 1 vote

That has endless ways and is mostly done by the personal preference of the programmer.

I like the most the one like thankfullheart shows (without that ! because that is VBA). 

But there are many who use 

TextBox1.Text=""
TextBox1.Text.Length = 0
String.IsNullOrEmpty(TextBox1.Text)
TextBox1.Text = String.Empty

Success
Cor


Saturday, September 28, 2013 9:49 AM

First many thanks Cor;)

Then I doubt I cannot say TextBox.Text.IsEmpty?And What do you mean "not in all frameworks"? I think Text's type is String, but String doesn't have such a method at all;)

I only see string's static method:IsNullOrEmpty……

So can you list some samples for us in details?

Click For donating:Free Rice For the poor
For spamming-sender issues, you can either report it at Microsoft Spamming Issue, or just find "Report Spam Here+Number" at Forum Issue;You can also find "Verify Your Account+Number" at "Forum Issue", where you can submit to be confirmed to paste links or images.
For more things to talk about? StackOverFlow is your choice.


Saturday, September 28, 2013 10:55 AM | 1 vote

And going with any method you desire as a personal choice if using VS2008 or higher we can make an extension method.

In this case IsNullOrWhiteSpace is Framework 4 or higher

<Runtime.CompilerServices.Extension()> _
Public Function IsEmpty(ByVal sender As TextBox) As Boolean
    Return String.IsNullOrWhiteSpace(sender.Text)
End Function

Usage

If TextBox1.IsEmpty Then
    MessageBox.Show("Empty")
Else
    MessageBox.Show(TextBox1.Text)
End If

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


Saturday, September 28, 2013 11:01 AM

        If TextBox1.Text = "" Then Debug.WriteLine("empty")

        If TextBox1.Text = String.Empty Then Debug.WriteLine("empty")

        If String.IsNullOrEmpty(TextBox1.Text) Then Debug.WriteLine("empty")

        If TextBox1.TextLength = 0 Then Debug.WriteLine("empty")

"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." JohnWein

Multics


Saturday, September 28, 2013 11:04 AM

Yes I should not be to sure of things, I never use it, I use the same method like you do.

Changed it.

Thanks
Cor


Sunday, September 29, 2013 12:56 AM

No problem Cor;)

I suspected whether there's a method like IsEmpty(string)……But I didn't find that at all……

Click For donating:Free Rice For the poor
For spamming-sender issues, you can either report it at Microsoft Spamming Issue, or just find "Report Spam Here+Number" at Forum Issue;You can also find "Verify Your Account+Number" at "Forum Issue", where you can submit to be confirmed to paste links or images.
For more things to talk about? StackOverFlow is your choice.