is a project of a calculator, and I can do the basic operations but entering data from the keyboard

0

I'm doing a project of a calculator, with basic operations

It works by entering data when pressing buttons with numbers and signs, but when doing keyboard operations it does not work for me.

Here is all the code I have:

Public Class Form1
    Dim dato As Double
    Dim dato2 As Double
    Dim resultado As Double
    Dim operacion As Double
    Dim memoria As Integer

    Private Sub Btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn0.Click
        If TextBox1.Text = "0" Then
            TextBox1.Clear()
        End If
        TextBox1.Text = TextBox1.Text & "0"
    End Sub

    Private Sub Btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn1.Click
        If TextBox1.Text = "0" Then
            TextBox1.Clear()
        End If
        TextBox1.Text = TextBox1.Text & "1"
    End Sub

    Private Sub Btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn2.Click
        If TextBox1.Text = "0" Then
            TextBox1.Clear()
        End If
        TextBox1.Text = TextBox1.Text & "2"
    End Sub

    Private Sub Btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn3.Click
        If TextBox1.Text = "0" Then
            TextBox1.Clear()
        End If
        TextBox1.Text = TextBox1.Text & "3"
    End Sub

    Private Sub Btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn4.Click
        If TextBox1.Text = "0" Then
            TextBox1.Clear()
        End If
        TextBox1.Text = TextBox1.Text & "4"
    End Sub

    Private Sub Btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn5.Click
        If TextBox1.Text = "0" Then
            TextBox1.Clear()
        End If
        TextBox1.Text = TextBox1.Text & "5"
    End Sub

    Private Sub Btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn6.Click
        If TextBox1.Text = "0" Then
            TextBox1.Clear()
        End If
        TextBox1.Text = TextBox1.Text & "6"
    End Sub

    Private Sub Btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn7.Click
        If TextBox1.Text = "0" Then
            TextBox1.Clear()
        End If
        TextBox1.Text = TextBox1.Text & "7"
    End Sub

    Private Sub Btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn8.Click
        If TextBox1.Text = "0" Then
            TextBox1.Clear()
        End If
        TextBox1.Text = TextBox1.Text & "8"
    End Sub

    Private Sub Btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn9.Click
        If TextBox1.Text = "0" Then
            TextBox1.Clear()
        End If
        TextBox1.Text = TextBox1.Text & "9"
    End Sub

    Private Sub Btnsuma_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnsuma.Click
        operacion = 1
        dato = dato + Val(TextBox1.Text)
        TextBox1.Clear()
    End Sub

    Private Sub Btnresta_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnresta.Click
        'TextBox1.Text = TextBox1.Text + "-"
        operacion = 2
        dato = dato + Val(TextBox1.Text)
        TextBox1.Clear()
    End Sub

    Private Sub Btnmultiplica_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnmultiplica.Click
        operacion = 3
        dato = Val(TextBox1.Text)
        TextBox1.Clear()
    End Sub

    Private Sub Btndivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btndivide.Click
        operacion = 4
        dato = Val(TextBox1.Text)
        TextBox1.Clear()
    End Sub

    Private Sub Btnexpo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnexpo.Click
        'operacion = 5
        dato = Val(TextBox1.Text)
        resultado = dato ^ 2
        TextBox1.Text = resultado
    End Sub

    Private Sub Btnraiz_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnraiz.Click
        dato = Val(TextBox1.Text)
        If dato < 0 Then
            MsgBox("opcion invalida")
        Else
            resultado = dato ^ (1 / 2)
            TextBox1.Text = resultado
        End If
    End Sub

    Private Sub Btnigual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnigual.Click
        dato2 = Val(TextBox1.Text)
        If operacion = 1 Then
            resultado = dato + dato2
            TextBox1.Text = resultado
            resultado = Val(TextBox1.Text)
        ElseIf operacion = 2 Then
            resultado = dato - dato2
            TextBox1.Text = resultado
            resultado = Val(TextBox1.Text)
        ElseIf operacion = 3 Then
            resultado = dato * dato2
            TextBox1.Text = resultado
            resultado = Val(TextBox1.Text)
        ElseIf operacion = 4 Then
            If dato2 = 0 Then
                TextBox1.Text = "ERROR"
                'MsgBox("NO SE PUEDE DIVIDIR ENTRE 0")
            Else
                resultado = dato / dato2
                TextBox1.Text = resultado
                resultado = Val(TextBox1.Text)
            End If
        End If

    End Sub

    Private Sub Btnborrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnborrar.Click
        If TextBox1.Text.Trim() <> "" Then
            TextBox1.Text =TextBox1.Text.Remove(TextBox1.Text.Length - 1, 1)
        End If
    End Sub

    Private Sub Btnlimpiar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnlimpiar.Click
        TextBox1.Clear()
        TextBox1.Text = "0"
    End Sub

    Private Sub Btnmemoria_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnmemoria.Click
        memoria = resultado
        TextBox1.Text = memoria
    End Sub

    Private Sub Btnpunto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnpunto.Click
        If InStr(TextBox1.Text, ".", CompareMethod.Text) = 0 Then
            TextBox1.Text &= "."
        End If
    End Sub


    Private Sub Buttonnegativo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonnegativo.Click
        If TextBox1.Text = "0" Then
            TextBox1.Clear()
        End If
        If InStr(TextBox1.Text, "-", CompareMethod.Text) = 0 Then
            TextBox1.Text = "-"
        End If
    End Sub

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)


    End Sub

    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If TextBox1.Text = "0" Then
            TextBox1.Clear()
        End If
        If Char.IsDigit(e.KeyChar) Or e.KeyChar = vbBack Or e.KeyChar = "." Or e.KeyChar = "/" Then
            e.Handled = False
        Else
            e.Handled = True

        End If
        If Asc(e.KeyChar) = 48 Then
            Btn0.PerformClick()
        ElseIf Asc(e.KeyChar) = 49 Then
            Btn1.PerformClick()
        ElseIf Asc(e.KeyChar) = 50 Then
            Btn2.PerformClick()
        ElseIf Asc(e.KeyChar) = 51 Then
            Btn3.PerformClick()
        ElseIf Asc(e.KeyChar) = 52 Then
            Btn4.PerformClick()
        ElseIf Asc(e.KeyChar) = 53 Then
            Btn5.PerformClick()
        ElseIf Asc(e.KeyChar) = 54 Then
            Btn6.PerformClick()
        ElseIf Asc(e.KeyChar) = 55 Then
            Btn7.PerformClick()
        ElseIf Asc(e.KeyChar) = 56 Then
            Btn8.PerformClick()
        ElseIf Asc(e.KeyChar) = 57 Then
            Btn9.PerformClick()
        ElseIf Asc(e.KeyChar) = 47 Then
            Btndivide.PerformClick()
        ElseIf Asc(e.KeyChar) = 43 Then
            Btnsuma.PerformClick()
        ElseIf Asc(e.KeyChar) = 45 Then
            Btnresta.PerformClick()
        ElseIf Asc(e.KeyChar) = 42 Then
            Btnmultiplica.PerformClick()
        ElseIf Asc(e.KeyChar) = 46 Then
            Btnpunto.PerformClick()
        ElseIf Asc(e.KeyChar) = 61 Then
            Btnigual.PerformClick()
        End If
    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Focus()
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
End Class
    
asked by Jorge Luque 30.09.2016 в 21:37
source

1 answer

1

The KeyPreview property allows the form to trigger the events associated with keys before they pass to the control that has the focus.

  

Form.KeyPreview (Property)

     

When this property is set to true , the form will receive   all events KeyPress , KeyDown , and KeyUp . After   that the form event handlers have finished   process the key press, it is assigned to the control with focus.

* I modified the original text a bit because it was a horrible translation.

Either by code:

Me.KeyPreview = True

or in the design of the Form

When configuring this property, all keys are processed from the form, regardless of the control that the focus has. In addition, you probably want the rest of the controls do not receive the key (avoiding, for example, printing duplicate). For that, the processing of the event stops with:

e.Handled = True


Note: If you want to intercept the keys at a lower (and more efficient) level, see Form.ProcessCmdKey . In this case it was omitted to simplify the response.

    
answered by 30.09.2016 в 22:22