[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2010-01-02
10
Medium Priority
?
272 Views
Last Modified: 2012-08-13
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
Comment
Question by:Zbriegnew
9 Comments
 
LVL 3

Expert Comment

by:DooDah
ID: 26164309

If KeyAscii = vbKeyReturn Then .....
0
 

Author Comment

by:Zbriegnew
ID: 26164343
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
 
LVL 3

Expert Comment

by:DooDah
ID: 26164370


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

End Sub
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Zbriegnew
ID: 26164409
DooDah,

Hmmm... Sorry, I am a bit of a starter, so I still am not sure where to place your suggestion...
0
 
LVL 83

Expert Comment

by:leakim971
ID: 26164449
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
 

Author Comment

by:Zbriegnew
ID: 26164463
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 26164521
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
 
LVL 3

Accepted Solution

by:
DooDah earned 2000 total points
ID: 26164722

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
 

Author Comment

by:Zbriegnew
ID: 26392035
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question