Results 1 to 3 of 3

Thread: [RESOLVED] If Textbox.Text = "" 'Wont Recognize Spaces!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2011
    Posts
    88

    Resolved [RESOLVED] If Textbox.Text = "" 'Wont Recognize Spaces!

    For a text-adventure game I basically have a series of If statements set up to make the game react appropriately based on a series of booleans and other variables as well as what the user has typed into the input text box.

    The problem is that I've just realised that VB won't recognize sentences, only words without spaces, dashes, underscore, etc!

    To be more clear, my problem is that while this works fine:

    vb Code:
    1. If Text1.Text = "North" Then
    2.               'Stuff
    3. ElseIf Text1.Text = "South" Then
    4. 'Other stuff"
    5. end if
    This won't work at all because of the spaces:
    vb Code:
    1. If Text1.Text = "Examine the chest" Then
    2.               'Stuff
    3. ElseIf Text1.Text = "Open the window" Then
    4. 'Other stuff
    5. end if

    Does anyone know of some way I can fix this?
    I would think there would have to be a way...

  2. #2
    Lively Member polecat's Avatar
    Join Date
    Jul 2005
    Location
    Wolverhampton
    Posts
    83

    Re: If Textbox.Text = "" 'Wont Recognize Spaces!

    The whole string wrapped in quotes is evaluated and this works,
    Code:
            If TextBox1.Text = "Examine the chest" Then
                MsgBox("Examine the chest is true")
    
            ElseIf TextBox1.Text = "Open the window" Then
                MsgBox("Open the window is true")
            End If
    if you are passing a string variable to a function then use Trim

    Code:
        Private Sub DoSomething(ByVal str As String)
    
            Dim TheString As String = Trim(str)
    
            Select Case TheString
    
                Case "Examine the chest"
                    MsgBox(TheString & " is true")
    
                Case "Open the window"
                    MsgBox(TheString & " is true")
    
            End Select
    
        End Sub
    Hope you get it running!

  3. #3
    Hyperactive Member
    Join Date
    Mar 2001
    Posts
    485

    Re: [RESOLVED] If Textbox.Text = "" 'Wont Recognize Spaces!

    Are the string from the textbox in proper capital like the strings that you are comparing with. Try using .CompareTo() method to eliminate the letter casing differences.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width