• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

How do I make the Enter key work as I want in VB 2008

I am working on a simple project using VB 2008 (Visual Studio). It should help children (and me) to memorize the times tables. The program generates two random numbers and when the user inserts their "guess" the software says "RIGHT" or "WRONG". The problem is that when you start typing, it already says "WRONG" before you hit the enter key. I want it to show these words only *after* the user presses the Enter key.

The code is this:

Public Class MEMORIZE TIMES TABLES

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim number1 As Integer
        Dim number2 As Integer
        Dim number3 As Integer

       TextBox3.Clear()

        Randomize()

        Do Until number1 >= 3 And number1 <= 13 And number1 <> 10
            number1 = Rnd() * 100
        Loop

        TextBox1.Text = number1.ToString

        Do Until number2 >= 3 And number2 <= 13 And number2 <> 10
            number2 = Rnd() * 100
        Loop

        TextBox2.Text = number2.ToString

        TextBox3.Focus()


    End Sub

    Private Sub TextBox3_(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged

        Dim result As Integer

        result = TextBox1.Text * TextBox2.Text

        If TextBox3.Text = result.ToString Then

            Label2.ForeColor = Color.Green

            Label2.Text = "RIGHT"

        Else

            Label2.ForeColor = Color.Red

            Label2.Text = "WRONG"

        End If
    End Sub

End Class
Public Class MEMORIZE TIMES TABLES

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim number1 As Integer
        Dim number2 As Integer
        Dim number3 As Integer

       TextBox3.Clear()

        Randomize()

        Do Until number1 >= 3 And number1 <= 13 And number1 <> 10
            number1 = Rnd() * 100
        Loop

        TextBox1.Text = number1.ToString

        Do Until number2 >= 3 And number2 <= 13 And number2 <> 10
            number2 = Rnd() * 100
        Loop

        TextBox2.Text = number2.ToString

        TextBox3.Focus()


    End Sub

    Private Sub TextBox3_(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged

        Dim result As Integer

        result = TextBox1.Text * TextBox2.Text

        If TextBox3.Text = result.ToString Then

            Label2.ForeColor = Color.Green

            Label2.Text = "RIGHT"

        Else

            Label2.ForeColor = Color.Red

            Label2.Text = "WRONG"

        End If
    End Sub

End Class

Open in new window

TIMES-TABLES.JPG
0
Zbriegnew
Asked:
Zbriegnew
1 Solution
 
DooDahCommented:

If KeyAscii = vbKeyReturn Then .....
0
 
ZbriegnewAuthor Commented:
DooDah,

VB 2008 says KeyAscii is not declared. Same goes for vbKeyReturn.

Should I declare them as integers?
If TextBox3.Text = result.ToString Then

            If KeyAscii = vbKeyReturn Then

                Label2.ForeColor = Color.Green

                Label2.Text = "CERTO"

            End If

Open in new window

0
 
DooDahCommented:


Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
   '...YOUR PROCEDURE CALL HERE
End If

End Sub
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
ZbriegnewAuthor Commented:
DooDah,

Hmmm... Sorry, I am a bit of a starter, so I still am not sure where to place your suggestion...
0
 
leakim971PluritechnicianCommented:
Hello Zbriegnew,

You shoud put you test in the Button1_Click Sub :

When you press ENTER this should emulate a click on Button1

Regards.
0
 
ZbriegnewAuthor Commented:
So, instead of

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

I should write:

Private Sub Button1_Click Form_KeyPress (KeyAscii As Integer)
If KeyAscii = 13 Then  ???
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Instead of TextChanged(), use the KeyPress() Event of TextBox3:
   Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
            Dim result As Integer

            result = TextBox1.Text * TextBox2.Text

            If TextBox3.Text = result.ToString Then

                Label2.ForeColor = Color.Green

                Label2.Text = "RIGHT"

            Else

                Label2.ForeColor = Color.Red

                Label2.Text = "WRONG"

            End If
        End If
    End Sub

Open in new window

0
 
DooDahCommented:

Agree with @Idle_Mind:

1.  ADD a Button2 to your FORM for SUBMIT on ENTER
2.  Paste in the following Code Changes for
               Private Sub Button2_Click
                                and
                If e.KeyChar = Chr(13) Then   Button2.PerformClick()
    This will give the user a multiple tries on the same problem, before proceeding to the next problem, also on success of the problem, you can also invoke "Button1.PerformClick()" to go to the next problem automatically

 
========================================================================

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim number1 As Integer
        Dim number2 As Integer
        Dim number3 As Integer

        TextBox3.Clear()

        Randomize()

        Do Until number1 >= 3 And number1 <= 13 And number1 <> 10
            number1 = Rnd() * 100
        Loop

        TextBox1.Text = number1.ToString

        Do Until number2 >= 3 And number2 <= 13 And number2 <> 10
            number2 = Rnd() * 100
        Loop

        TextBox2.Text = number2.ToString

        TextBox3.Focus()

    End Sub



    ' Button 1 is the NEXT Problem Button,  Button 2 is the SUBMIT Button ( Can Re-Check as well before going to next problem )
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim result As Integer

        result = TextBox1.Text * TextBox2.Text

        If TextBox3.Text = result.ToString Then

            Label2.ForeColor = Color.Green

            Label2.Text = "RIGHT"

        Else

            Label2.ForeColor = Color.Red

            Label2.Text = "WRONG"

        End If
    End Sub



    Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
        If e.KeyChar = Chr(13) Then
            Button2.PerformClick() Button 2 is the SUBMIT Button ( Can Re-Check as well before Pressing Button1 to go to the next problem )
        End If
    End Sub
End Class
0
 
ZbriegnewAuthor Commented:
Sorry for the delay. A lot of personal problems came up these last days. Anyway, I am now trying to find out how to split the points as suggested.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now