Como hacer una Calculadora en Visual Basic .Net (vb)

Hola amigos:

Ahora continuare con los programas hechos en Visual Basic .Net 2005 los cuales hoy en día si son de mucha utilidad ya que este entorno de desarrollo se esta utilizando mucho y por lo tanto estos programas te pueden sacar de alguna duda así que los publicare :P

Este programa es una calculadora muy similar a la que viene con el Windows XP o el Windows Vista. Contiene los mismos botones y funciona muy parecido.

Al correrlo, el programa luce así:

y el código es este (al final puedes descargar los archivos):

[vbnet]
Public Class Form1
Function buscar(ByVal txtval As String, ByVal car As Char) As Boolean
Dim b As Integer
For b = 1 To txtval.Length
If Convert.ToChar(Mid(txtval, b, 1)) = car Then
Return False
End If
Next
Return True
End Function
Dim val1, val2 As Double
Dim banR, ban, ban_br As Integer
Dim oper As String
Private Sub txt0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt0.Click
If banR = 0 Then
r.Text &= “0″
Else
r.Text = “0″
End If
End Sub

Private Sub txt1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt1.Click
If banR = 0 Then
r.Text &= “1″
Else
r.Text = “1″
End If
End Sub

Private Sub txt2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt2.Click
If banR = 0 Then
r.Text &= “2″
Else
r.Text = “2″
End If
End Sub

Private Sub txt3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt3.Click
If banR = 0 Then
r.Text &= “3″
Else
r.Text = “3″
End If
End Sub

Private Sub txt4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt4.Click
If banR = 0 Then
r.Text &= “4″
Else
r.Text = “4″
End If
End Sub

Private Sub txt5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt5.Click
If banR = 0 Then
r.Text &= “5″
Else
r.Text = “5″
End If
End Sub

Private Sub txt6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt6.Click
If banR = 0 Then
r.Text &= “6″
Else
r.Text = “6″
End If
End Sub

Private Sub txt7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt7.Click
If banR = 0 Then
r.Text &= “7″
Else
r.Text = “7″
End If
End Sub

Private Sub txt8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt8.Click
If banR = 0 Then
r.Text &= “8″
Else
r.Text = “8″
End If
End Sub

Private Sub txt9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt9.Click
If banR = 0 Then
r.Text &= “9″
Else
r.Text = “9″
End If
End Sub

Private Sub txtce_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtce.Click
oper = “”
val2 = 0
r.Text = “”
banR = 0
ban_br = 0
r.Focus()
End Sub

Private Sub txtc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtc.Click
banR = 0
ban = 0
ban_br = 0
oper = “”
val1 = 0
val2 = 0
r.Text = “”
r.Focus()
End Sub

Private Sub txtigual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtigual.Click
val2 = Val(r.Text)
Select Case oper
Case “+”
val1 += val2
Case “-”
val1 -= val2
Case “*”
val1 *= val2
Case “/”
val1 /= val2
End Select
r.Text = val1
banR = 1
ban = 1
ban_br = 1
End Sub

Private Sub txtadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtadd.Click
If ban = 0 Then
val1 = Val(r.Text)
oper = “+”
r.Text = “”
r.Focus()
ban = 1
Else
val2 = Val(r.Text)
If ban_br = 1 Then ‘si ya presiono el boton de =
ban_br = 0
Else
Select Case oper
Case “+”
val1 += val2
Case “-”
Case “*”
Case “/”
End Select
End If
oper = “+”
r.Text = val1
banR = 1
End If
End Sub

Private Sub txtsub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsub.Click
If ban = 0 Then
val1 = Val(r.Text)
oper = “-”
r.Text = “”
r.Focus()
ban = 1
Else
val2 = Val(r.Text)
If ban_br = 1 Then ‘si ya presiono el boton de =
ban_br = 0
Else
Select Case oper
Case “+”
val1 += val2
Case “-”
Case “*”
Case “/”
End Select
End If
oper = “-”
r.Text = val1
banR = 1
End If
End Sub

Private Sub txtmul_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtmul.Click
If ban = 0 Then
val1 = Val(r.Text)
oper = “*”
r.Text = “”
r.Focus()
ban = 1
Else
val2 = Val(r.Text)
If ban_br = 1 Then ‘si ya presiono el boton de =
ban_br = 0
Else
Select Case oper
Case “+”
val1 += val2
Case “-”
Case “*”
Case “/”
End Select
End If
oper = “*”
r.Text = val1
banR = 1
End If
End Sub

Private Sub txtdiv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtdiv.Click
If ban = 0 Then
val1 = Val(r.Text)
oper = “/”
r.Text = “”
r.Focus()
ban = 1
Else
val2 = Val(r.Text)
If ban_br = 1 Then ‘si ya presiono el boton de =
ban_br = 0
Else
Select Case oper
Case “+”
val1 += val2
Case “-”
Case “*”
Case “/”
End Select
End If
oper = “/”
r.Text = val1
banR = 1
End If
End Sub

Private Sub txtpunto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtpunto.Click
If buscar(r.Text, “.”) = True Then
r.Text &= “.”
End If
End Sub
End Class
[/vbnet]

Descarga

Share Button
Latest Comments

Leave a Reply

Tu dirección de correo electrónico no será publicada. Los campos necesarios están marcados *