Está en la página 1de 4

Prctica 4 CALCULADORA VALIDADA

Este programa representa una calculadora bsica capaz de hacer sumas, restas multiplicaciones y divisiones. Cuando se inserte algn dato diferente a nmeros, en los textboxs mostrar una ventana que indica error de correccin en los textboxs. PROCEDIMIENTO El programa no necesita que se declaren variables, porque se trabajar directamente con los valores que se introduzcan en los textbox. Se condicionarn con el comando if los valores de los textbox, si el recuadro esta en blanco, aparecer una ventana marcando error. Para llevar a cabo alguna operacin debe seleccionarse la opcin que se desee, luego, el programa tomar los valores introducidos en los textbox y realizar las operaciones, mostrando el resultado. Si los textbox no tienen ningn valor, o si no se ha seleccionado alguna opcin de operacin, se mostrar una ventana que indique error.

CDIGO CALCULADORA
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

If TextBox3.Text = "suma" Then TextBox4.Text = Val(TextBox1.Text) + Val(TextBox3.Text) ElseIf TextBox3.Text = "resta" Then TextBox4.Text = Val(TextBox1.Text) - Val(TextBox3.Text) ElseIf TextBox3.Text = "multiplicacion" Then TextBox4.Text = Val(TextBox1.Text) * Val(TextBox3.Text) ElseIf TextBox3.Text = "division" Then TextBox4.Text = Val(TextBox1.Text) / Val(TextBox3.Text) Else : TextBox4.Text = "ERROR" End If End Sub End Class

Para validar la calculadora se utilizar el siguiente cdigo:


Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown If Not ((e.KeyValue >= 48 And e.KeyValue <= 57) OrElse (e.KeyValue >= 96 And e.KeyValue <= 105) OrElse (e.KeyValue = 0.8)) Then e.Handled = True MsgBox("Favor de introducir solamente numeros GRACIAS", MsgBoxStyle.Exclamation) TextBox1.Text = vbNullChar End If End Sub

CDIGO CALCULADORA VALIDADA

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

If TextBox3.Text = "suma" Then TextBox4.Text = Val(TextBox1.Text) + Val(TextBox3.Text) ElseIf TextBox3.Text = "resta" Then TextBox4.Text = Val(TextBox1.Text) - Val(TextBox3.Text) ElseIf TextBox3.Text = "multiplicacion" Then TextBox4.Text = Val(TextBox1.Text) * Val(TextBox3.Text) ElseIf TextBox3.Text = "division" Then TextBox4.Text = Val(TextBox1.Text) / Val(TextBox3.Text) Else : TextBox4.Text = "ERROR" End If

End Sub Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown If Not ((e.KeyValue >= 48 And e.KeyValue <= 57) OrElse (e.KeyValue >= 96 And e.KeyValue <= 105) OrElse (e.KeyValue = 0.8)) Then e.Handled = True MsgBox("Favor de introducir solamente numeros GRACIAS", MsgBoxStyle.Exclamation) TextBox1.Text = vbNullChar End If

End Sub Private Sub TextBox3_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox3.KeyDown If Not ((e.KeyValue >= 48 And e.KeyValue <= 57) OrElse (e.KeyValue >= 96 And e.KeyValue <= 105) OrElse (e.KeyValue = 0.8)) Then e.Handled = True MsgBox("Favor de introducir solamente numeros GRACIAS", MsgBoxStyle.Exclamation) TextBox3.Text = vbNullChar End If End Sub End Class

PROGRAMA

Ventana principal del programa Al ser una calculadora validada, solo aceptar nmeros, en caso contrario aparecer la ventana siguiente:

La operacin que se llevar a cabo debe realizarse de la siguiente manera:

Si en el textbox donde se selecciona la operacin no corresponde a las opciones, se mostrar una ventana de error:

También podría gustarte