tags:

views:

84

answers:

2

Hey guys,

I'm making a SIMPLE, and when I mean simple, I mean simple. But any way, I have this thing where, the person enters in a word, and it will preview a text file. I have everything setup even the text reader, but something unusual I get in my code is the "if statement". Whenever I type in the word into the textbox, it won't run the thing I want it to, instead it will go directly to the else and display the error message. This is the code I have:

If mainText.Text = "book" Then
        startProcess()

    ElseIf mainText.Text = "greet" Then  //Just there for testing..
        mainText.Text = "Hello there..."
    Else
        mainText.Text = "Please either enter a valid command."
    End If

Now i looked into mainText.text and saw the value when entering the greet command..

{System.Windows.Forms.TextBox, Text: greet" & vbCrLf & "}

.. I don't really understand why this is happening.. All my other program work fine.

Can anyone help me solve this!??!

Thanks,

Kevin

+1  A: 

Is the textbox multi-line? A single-line textbox shouldn't allow these characters. Either way, you can probably just trim the string.

Dim command As String = mainText.Text.Trim()
If command = "book" Then
    startProcess()
ElseIf command = "greet" Then  // Just there for testing..
    mainText.Text = "Hello there..."
Else
    mainText.Text = "Please either enter a valid command."
End If
Snarfblam
OK this is getting even weider... Yes my textbox is a multiline textbox, but when I do this, it doesn't fix the problem completely.. When I put a breakpoint on the problem and continue it, it works! WHY ISN'T IT WORKING WHEN WITHOUT A BREAKPOINT OR PAUSE?!!!
Kevin
By the way, I use a timer when my enter key is pressed to go into that sub above..
Kevin
You might consider editing the question with your current code, unless it looks identical to mine. I haven't tested what I posted, though I probably should have said so.
Snarfblam
Yup it looks identical..
Kevin
A: 

The fact that it works when you break it sounds like a synchronization problem (multithreading) .. could be something connected to the timer you use. Can you provide more information regarding your code/logic?

Ando
So it works like this.. I have a form and one single textbox in the smack dad middle of it. I also have a timer running to see when the enter key is pressed so it can run the action above, and display something in return. It's pretty simple..
Kevin
Why use a timer to check for the enter key? Are you checking for the enter key in the textbox? If so, just handle the normal textbox keyboard events (KeyDown, KeyUp, or KeyPress)
Chris Dunaway
Considering that it only works when stopped at a breakpoint, I would say the timer is probably related to the inconsistent results. The timer is also completely unnecessary when you have your normal key events mentioned by Chris.
Snarfblam
Could you please log what actual is happening (for example put some logging in each logic branch and log the mainText.Text)? It could be that you catch the Enter event multiple times and the last time you catch it it goes on the last else branch.
Ando
Yea, I think the timer was the problem. I'm going to try the keydown event to see if that works...
Kevin