Key Press Help

Sebby123

New member
Joined
Jun 4, 2007
Messages
2
Programming Experience
Beginner
Hey, Im Makeing A Caclulator For My School Project And I Wanted To Make It Good By Having It Enter The Numbers Buy Keypress.

Ive Searched And I Dont Realy Understand The Whole Concept Kind Of Thing

I Just Want It So If I Press The Numpad1 Then It Writes 1 In The TextBox

I Made The TextBox Read Only To Stop People Putting In Text
 
I would use the Form's KeyUp event to get the key that was pressed:

VB.NET:
Private sub Form_KeyUp (...) Handles Me.KeyUp
  Select Case e.KeyCode
    Case Keys.1
      TextBox.Text &= "1"
    Case Keys.2
      TextBox.Text &= "2"
  End Select
End Sub
 
I believe the better solution would be to disable the ability to enter anything but numbers in the textbox. You can do this several ways; one is to handle one of the Key events for the textbox and check to see if the input is a number. If so, allow the input, if not, disallow it (e.Handled = True).
The function of a textbox after all is to accept user input from the keyboard.
 
VB.NET:
[URL="http://four.fsphost.com/Sebby123/Calc.vb"]The Source Is Here[/URL]
[URL="http://four.fsphost.com/Sebby123/Calc.Designer.vb"]And Here[/URL]
help1cy8.jpg


It Gave Me Several Errors...

And What Shall I Put In The Forum_KeyUp (Whats In Here?)

Im New To VB.. Please Help :)

Ive Tryed Again And Think Im Almost There... Can You Look At This Code And See What Im Doing Wrong.

VB.NET:
    Private Sub Blah_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.D0) Then TextBox1.Text = TextBox1.Text + "0"
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.D1) Then TextBox1.Text = TextBox1.Text + "1"
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.D2) Then TextBox1.Text = TextBox1.Text + "2"
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.D3) Then TextBox1.Text = TextBox1.Text + "3"
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.D4) Then TextBox1.Text = TextBox1.Text + "4"
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.D5) Then TextBox1.Text = TextBox1.Text + "5"
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.D6) Then TextBox1.Text = TextBox1.Text + "6"
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.D7) Then TextBox1.Text = TextBox1.Text + "7"
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.D8) Then TextBox1.Text = TextBox1.Text + "8"
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.D9) Then TextBox1.Text = TextBox1.Text + "9"
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Decimal) Then TextBox1.Text = TextBox1.Text + "."
        DecPoint.Enabled = False
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Add) Then Label2.Text = TextBox1.Text
        Label3.Text = "Plus"
        TextBox1.Clear()
        DecPoint.Enabled = True
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Subtract) Then Label2.Text = TextBox1.Text
        Label3.Text = "Take"
        TextBox1.Clear()
        DecPoint.Enabled = True
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Multiply) Then Label2.Text = TextBox1.Text
        Label3.Text = "Times"
        TextBox1.Clear()
        DecPoint.Enabled = True
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Divide) Then Label2.Text = TextBox1.Text
        Label3.Text = "Divide"
        TextBox1.Clear()
        DecPoint.Enabled = True
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then If Label3.Text = "Plus" Then 

        If Label3.Text = "Plus" Then
            Dim Num1 As Decimal
            Dim Num2 As Decimal
            Num1 = Label2.Text
            Num2 = TextBox1.Text
            Dim Awn = Num1 + Num2
            TextBox1.Text = Awn
        End If

        If Label3.Text = "Take" Then
            Dim Num1 As Decimal
            Dim Num2 As Decimal
            Num1 = Label2.Text
            Num2 = TextBox1.Text
            Dim Awn = Num1 - Num2
            TextBox1.Text = Awn
        End If

        If Label3.Text = "Times" Then
            Dim Num1 As Decimal
            Dim Num2 As Decimal
            Num1 = Label2.Text
            Num2 = TextBox1.Text
            Dim Awn = Num1 * Num2
            TextBox1.Text = Awn
        End If

        If Label3.Text = "Divide" Then
            Dim Num1 As Decimal
            Dim Num2 As Decimal
            Num1 = Label2.Text
            Num2 = TextBox1.Text
            Dim Awn = Num1 / Num2
            TextBox1.Text = Awn
        End If

        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.NumPad0) Then TextBox1.Text = TextBox1.Text + "0"
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.NumPad1) Then TextBox1.Text = TextBox1.Text + "1"
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.NumPad2) Then TextBox1.Text = TextBox1.Text + "2"
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.NumPad3) Then TextBox1.Text = TextBox1.Text + "3"
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.NumPad4) Then TextBox1.Text = TextBox1.Text + "4"
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.NumPad5) Then TextBox1.Text = TextBox1.Text + "5"
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.NumPad6) Then TextBox1.Text = TextBox1.Text + "6"
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.NumPad7) Then TextBox1.Text = TextBox1.Text + "7"
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.NumPad8) Then TextBox1.Text = TextBox1.Text + "8"
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.NumPad9) Then TextBox1.Text = TextBox1.Text + "9"

    End Sub
 
Last edited:
You have a lot of 'If Then' structures in that code, maybe you should try to
work with a 'Select Case True', It's a bit cleaner imho. :)
 
Last edited:
Back
Top