
Calculadora por teclado
Publicado por Julian C. (14 intervenciones) el 04/05/2016 11:40:59
Hola amigos, espero estén bien.
Mi duda es la siguiente: estoy creando una calculadora en VB 2008, he logrado crear el código con sus funciones con un teclado virtual que funciona muy bien con comand buttons, la pregunta es la siguiente, todo funciona bien hasta que trato de ingresar los datos desde el teclado, tengo el siguiente código:
Un textbox, 10 botones del 0 al 9, 4 botones para las operaciones aritméticas básicas Etc...
Les agradecería que me ayudaran con esto rápido...
Mi duda es la siguiente: estoy creando una calculadora en VB 2008, he logrado crear el código con sus funciones con un teclado virtual que funciona muy bien con comand buttons, la pregunta es la siguiente, todo funciona bien hasta que trato de ingresar los datos desde el teclado, tengo el siguiente código:
Un textbox, 10 botones del 0 al 9, 4 botones para las operaciones aritméticas básicas Etc...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
Imports System.Math
Imports Microsoft.VisualBasic.Strings
Public Class Form1
Dim x As Double, y As Double, z As String, M As Double, done As Boolean
'Rutina de ejecución(=):
Sub doit()
With TextBox1
y = Val(TextBox1.Text)
Select Case z
Case "+" : .Text = Str(x + y)
Case "-" : .Text = Str(x - y)
Case "*" : .Text = Str(x * y)
Case "/" : If y <> 0 Then .Text = Str(x / y) Else .Text = "Algo va mal aquí, ingrese un valor válido"
Case "E"
.Text = Val(.Text.Substring(0, InStr(.Text, "E") - 1)) * (10 ^ Val(.Text.Substring(InStr(.Text, "E"))))
Case "XRY" : .Text = Str(y ^ (1 / x))
'Nuevo bloque
Case Keys.Add : .Text = Str(x + y)
Case Keys.Subtract : .Text = Str(x - y)
Case Keys.Multiply : .Text = Str(x * y)
Case Keys.Divide : .Text = Str(x / y)
End Select
x = Val(TextBox1.Text) : y = 0 : z = ""
done = True
End With
End Sub
Private Sub textbox1_Keypress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
Select Case e.KeyCode
Case Keys.Multiply
z = "*"
doit()
End Select
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
numtst(2)
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
numtst(4)
End Sub
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
numtst(8)
End Sub
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
numtst(9)
End Sub
'Fin del código standard
'#########################################################################
'########################################################################
Sub numtst(ByVal a As Byte)
With TextBox1
If .Text = "0" Or done = True Then
.Text = ""
done = False
End If
.Text = .Text & Str(a).Substring(1)
End With
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Keys.Enter Then
doit()
End If
Label1.Text = TextBox1.Text
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
numtst(1)
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Salir.Click
End
End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Form2.Click
'Limpiando las secuencias de ejecución:
x = 0 : y = 0 : z = "" : TextBox1.Text = "0" : done = False
TextBox1.Text = ""
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
numtst(5)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
numtst(3)
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
numtst(6)
End Sub
Private Sub Button7_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
numtst(7)
End Sub
Private Sub Button10_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
doit()
z = "*"
End Sub
Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
doit()
z = "-"
End Sub
Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
doit()
z = "+"
End Sub
Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
numtst(0)
End Sub
Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
doit()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Visible = True
TextBox1.Focus()
End Sub
End Class
Les agradecería que me ayudaran con esto rápido...
Valora esta pregunta


0