Question How to check if the string in textbox has space/spaces

tresehulyo

Member
Joined
Sep 1, 2009
Messages
16
Programming Experience
Beginner
How to check if the string in textbox has space/spaces

then i will put it in if condition
 
It depends are you looking for any spaces including appearing between words (if so see above post) or are you validating that the textbox doesnt contain only spaces? If the latter try the following.

VB.NET:
    [COLOR="Blue"]If [/COLOR]TextBox1.Text.Trim.Length = 0 [COLOR="blue"]Then[/COLOR]
        [COLOR="SeaGreen"]    'Text box is empty of any text[/COLOR]
    [COLOR="blue"]End If[/COLOR]
 
Last edited:
Just put the next code into the button part:
If TextBox1.Text.Contains(" ") = True Then
MessageBox.Show("You have spaces")
ElseIf TextBox1.Text.Contains(" ") = False Then
MessageBox.Show("You have no spaces")
End If
You can always edit the text for other functions.

Greetz, Jojo
 
No reason to check twice....

VB.NET:
    [COLOR="Blue"]If [/COLOR]TextBox1.Text.Contains(" ") = True [COLOR="blue"]Then[/COLOR]
        MessageBox.Show([COLOR="Red"]"You have spaces"[/COLOR])
    [COLOR="blue"]Else[/COLOR]
        MessageBox.Show([COLOR="Red"]"You have no spaces"[/COLOR])
    [COLOR="blue"]End If[/COLOR]
 
No reason to check twice....

VB.NET:
    [COLOR="Blue"]If [/COLOR]TextBox1.Text.Contains(" ") = True [COLOR="blue"]Then[/COLOR]
        MessageBox.Show([COLOR="Red"]"You have spaces"[/COLOR])
    [COLOR="blue"]Else[/COLOR]
        MessageBox.Show([COLOR="Red"]"You have no spaces"[/COLOR])
    [COLOR="blue"]End If[/COLOR]

True, I'm sorry but I'm quite new to VB :eek:
 
No problem, just pointing out different ways of doing things. I come here for the same, to learn from the differences of others :)
 
Back
Top