Hi. I'm making a program somewhat like Siri except through text boxes not voice command. On my form FormMain I have two text boxes named TextBoxInput and TextBoxConversation. I also have two buttons named ButtonStart and ButtonEnd. Here is my entire code for FormMain so far:

Public Class FormMain

    Dim yourname As String

    Private Sub ButtonStart_Click(sender As Object, e As EventArgs) Handles ButtonStart.Click
        TextBoxInput.Enabled = True
        Call Conversation()
    End Sub

    Private Sub ButtonEnd_Click(sender As Object, e As EventArgs) Handles ButtonEnd.Click
        TextBoxInput.Enabled = False

    End Sub

    Sub Conversation()
        TextBoxConversation.Text = "Hi! My name is Alex." + vbCrLf + "To end our conversation you may click the ""End Conversation"" button at the bottom of the app at anytime." + vbCrLf + "What is your name?"
        yourname = TextBoxInput.Text
        TextBoxConversation.Text = TextBoxConversation.Text + vbCrLf + yourname + vbCrLf + "Nice to meet you " + yourname + ". How are you?"

    End Sub
End Class

What I need to do is when TextBoxConversation asks for a name, I need it to wait for the enter key to be pressed, and then set yourname equal to TextBoxInput.Text. I tried doing this but it gave me errors in the sub Conversation when I tried to Call the sub FormMain_EnterKeyUp:

Public Class FormMain

    Dim yourname As String

    Private Sub ButtonStart_Click(sender As Object, e As EventArgs) Handles ButtonStart.Click
        TextBoxInput.Enabled = True
        Call Conversation()
    End Sub

    Private Sub ButtonEnd_Click(sender As Object, e As EventArgs) Handles ButtonEnd.Click
        TextBoxInput.Enabled = False

    End Sub

    Sub Conversation()
        TextBoxConversation.Text = "Hi! My name is Alex." + vbCrLf + "To end our conversation you may click the ""End Conversation"" button at the bottom of the app at anytime." + vbCrLf + "What is your name?"
        Call FormMain_EnterKeyUp() *<--Error was here*
        yourname = TextBoxInput.Text
        TextBoxConversation.Text = TextBoxConversation.Text + vbCrLf + yourname + vbCrLf + "Nice to meet you " + yourname + ". How are you?"

    End Sub

    Sub FormMain_EnterKeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
        Do Until e.KeyCode = Keys.Enter
        Loop
    End Sub

I just need to call the sub and make it do nothing until the enter key is pressed, and when it is pressed, I need it to go back to sub Conversation and continue where it left off. And then repeat the call function each time a new question is asked like this:

TextBoxConversation.Text = TextBoxConversation.Text + vbCrLf + "How are you?"
Call Conversation()
yourstatus = TextBoxInput.Text

'React to the user's answer here

Is this possible? Thanks in advance.

-David

Handle the KeyUp event of the textbox not the form. Then call Conversation when the key code is Keys.Enter:

Private Sub TextBoxInput_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBoxInput.KeyUp
    If e.KeyCode = Keys.Enter Then
        Conversation
    End If
End Sub

Is there a way I can call different parts of the sub? Something like this?:

Sub Conversation()
        TextBoxConversation.Text = "Hi! My name is Alex." + vbCrLf + "To end our conversation you may click the ""End Conversation"" button at the bottom of the app at anytime." + vbCrLf + "What is your name?"
        Call TextBoxInput_KeyUp()
        TextBoxConversation.Text = TextBoxConversation.Text + vbCrLf + yourname + vbCrLf + "Nice to meet you " + yourname + ". How are you?"

End Sub

Private Sub TextBoxInput_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBoxInput.KeyUp
    If e.KeyCode = Keys.Enter Then
        Conversation('at line after this sub was called')
    End If
End Sub

You could set an argument for the sub. Maybe an integer. Then in the sub use a Select-Case block to choose what you want it to do next, based on the number that gets passed to the sub.

d1d3e9f38b90926de78db4a424f992d9

There's a visual for you.
What I want to happen when I click ButtonStart is TextBoxInput to be enabled and the computer to start the conversation in TextBoxConversation, like this:

80c8be05ff32029d6b5ed1f7ec647ba6

And then when I hit enter, TextBoxConversation will say "Nice to meet you David. How are you?"

Here's my code:

Public Class FormMain

    Dim yourname As String

    Private Sub ButtonStart_Click(sender As Object, e As EventArgs) Handles ButtonStart.Click
        TextBoxInput.Enabled = True
        Call TextBoxInput_Conversation()
    End Sub

    Private Sub ButtonEnd_Click(sender As Object, e As EventArgs) Handles ButtonEnd.Click
        TextBoxInput.Enabled = False

    End Sub

    Sub Conversation()


    End Sub

    Sub FormMain_EnterKeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
        Do Until e.KeyCode = Keys.Enter
        Loop
    End Sub

    Sub TextBoxInput_Conversation(sender As Object, e As EventArgs) Handles Me.KeyUp
        TextBoxConversation.Text = "Hi! My name is Alex." + vbCrLf + "To end our conversation you may click the ""End Conversation"" button at the bottom of the app at anytime." + vbCrLf + "What is your name?"
        Do Until e.keycode = Keys.Enter
        Loop
        TextBoxConversation.Text = TextBoxConversation.Text + vbCrLf + yourname + vbCrLf + "Nice to meet you " + yourname + ". How are you?"
    End Sub
End Class

Here is my error report when I try to run the program:

------ Build started: Project: Alex, Configuration: Debug Any CPU ------
C:\Users\David\Documents\Visual Studio 2012\Projects\Alex\Alex\FormMain.vb(7) : error BC30455: Argument not specified for parameter 'e' of 'Public Sub TextBoxInput_Conversation(sender As Object, e As System.EventArgs)'.
C:\Users\David\Documents\Visual Studio 2012\Projects\Alex\Alex\FormMain.vb(7) : error BC30455: Argument not specified for parameter 'sender' of 'Public Sub TextBoxInput_Conversation(sender As Object, e As System.EventArgs)'.
C:\Users\David\Documents\Visual Studio 2012\Projects\Alex\Alex\FormMain.vb(27) : error BC30456: 'keycode' is not a member of 'System.EventArgs'.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

You're trying to call TextBoxInput_Conversation() without any of the arguments that it requires.

Oh. So what would I put in the () when I call it??

just a suggestion :
i think it will work if you're trying to put the whole conversation on the TextBoxConversation.

Sub FormMain_EnterKeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
        Do Until e.KeyCode = Keys.Enter
            Sub Conversation()
        Loop
End Sub

-HappyCoding

I don't think that will do anything :/

So I was thinking about your post on the bus from school and I started to realize what you were saying. I thought you meant call the sub in the Do Loop command, but you were just saying put the code for the conversation in there and it works. The only error I get now is when I call the sub. Would someone help me with the arguments? Sorry for the trouble, I'm a pain in the butt I know, but I'm pretty new to programming.

Updated code:

Public Class FormMain

    Dim yourname As String

    Private Sub ButtonStart_Click(sender As Object, e As EventArgs) Handles ButtonStart.Click
        TextBoxInput.Enabled = True
        Call TextBoxInput_Conversation('sender As Object, e As KeyEventArgs')
    End Sub

    Private Sub ButtonEnd_Click(sender As Object, e As EventArgs) Handles ButtonEnd.Click
        TextBoxInput.Enabled = False

    End Sub

    Sub TextBoxInput_Conversation(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
        Do Until e.KeyCode = Keys.Enter
            TextBoxConversation.Text = "Hi! My name is Alex." + vbCrLf + "To end our conversation you may click the ""End Conversation"" button at the bottom of the app at anytime." + vbCrLf + "What is your name?"
            Threading.Thread.Sleep(30000)
        Loop
        TextBoxConversation.Text = TextBoxConversation.Text + vbCrLf + yourname + vbCrLf + "Nice to meet you " + yourname + ". How are you?"
    End Sub
End Class

Error Report:

------ Build started: Project: Alex, Configuration: Debug Any CPU ------
C:\Users\David\Documents\Visual Studio 2012\Projects\Alex\Alex\FormMain.vb(7) : error BC30201: Expression expected.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Something like this would work:

TextBoxInput_Conversation(Me, New KeyEventArgs())

I should caution this is probably not the best way to accomplish your goal. You should probably look at examples of messenger apps and adapt one to your needs.

The error always happens before the ' in line 7 saying "Expression Expected".

Have you tried following my last post?

Yes actually I didn't see that post until right now. I had my dad look at the code with me and this is what we put in the ():

(Me, New KeyEventArgs(Keys.A))

But now there's other errors and it just keeps looping and looping without giving the user a chance to type much. So he said that he'd help me later with it but I will look into messenger apps. Thanks for your help.

-David

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.