how do i tell if a text box contains only spaces?

I am just converting from VB 6 to Express, and alot of the coding is different, so I've been messing around with some basic programs to get the hang of it. I'm currently working on a Magic 8 Ball Program lol, but it is actually relatively in-depth (for a magic 8 ball program lol). but my question is, how can I tell if a text box contains only spaces, because if the user clicks the button, i don't want the button to function unless they have typed something into the text box. so i was going to use a simple if then else statement, but i still can't figure out how to figure out how to tell if the textbox contains just spaces for my if part. does it have anything to do with trim can someone please give me the code for this

thanks


Answer this question

how do i tell if a text box contains only spaces?

  • StephenLiang

    Just as a question on the string.IsNullOrEmpty - and I dont doubt the validity of using it.

    As a string will terminate on a null character and trim will remove any blank characters from the front and back of the string.     If its empty then the text.trim.length will be 0 so you shouldnt get 'something other than blank'  as its 0 it should definately show 'Blank Only'

    I've tried the original code purposely setting the Textbox.text  property to

    "    "
    chr(0)
    "    " & chr(0)
    ""
    Nothing
    String.Empty

    and the original code works just fine.

    Can you point me to the scenario as to a textbox being set to something where the original code wouldnt work.   I curious now .


  • Ajay Sudan

    *sigh* I think I was a little hasty, on second thoughts, I don't think it's needed at all.

    So, you were right again :-)



  • shayke

    I was just curious. I'd not seen that function before so youve enlightened me today with a new method in the framework.


  • mk_one

    How about the following piece of code.

    IF Textbox1.Text.Trim.Length = 0 THEN
    msgbox("Blank Only")
    ELSE
    msgbox("Something other than blank space")
    END IF


  • kikoman8

    How about this

    IF Not string.IsNullOrEmpty(Textbox1.Text) AND Textbox1.Text.Trim.Length = 0 THEN
    msgbox("Blank Only")
    ELSE
    msgbox("Something other than blank space")
    END IF

    Otherwise, you get 'something other than blank space' when it's empty.



  • Mustafa AYGUN

    *grin* that's cool. I found out about it on these forums as well.



  • how do i tell if a text box contains only spaces?