Jump to content

Preciso de ajuda para fazer uma calculadora


arom98
 Share

Recommended Posts

boas, eu fiz uma calculadora no VB2015, mas nao consigo que ela funcione, quem puder dar uma ajuda agradecia.

obrigado

Public Class Form1
Dim num1 As Double
Dim sinal As String

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles ButtonC.Click
 TextBox1.Text = ""
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
 If num1 = "1" Or "2" Or "3" Or "4" Or "5" Or "6" Or "7" Or "7" Or "8" Or "9" Or "0" Then
	 TextBox1.Text = "7"
 Else
	 num1 = "7"
 End If
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
 If num1 = "1" Or "2" Or "3" Or "4" Or "5" Or "6" Or "7" Or "7" Or "8" Or "9" Or "0" Then
	 TextBox1.Text = "8"
 Else
	 num1 = "8"
 End If
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
 TextBox1.Text = "+"
 sinal = TextBox1.Text
End Sub
Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
 If num1 = "1" Or "2" Or "3" Or "4" Or "5" Or "6" Or "7" Or "7" Or "8" Or "9" Or "0" Then
	 TextBox1.Text = "6"
 Else
	 num1 = "6"
 End If
End Sub
Private Sub sair_Click(sender As Object, e As EventArgs) Handles sair.Click
 End
End Sub
Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
 If num1 = "1" Or "2" Or "3" Or "4" Or "5" Or "6" Or "7" Or "7" Or "8" Or "9" Or "0" Then
	 TextBox1.Text = "0"
 Else
	 num1 = "0"
 End If
End Sub
Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
 If num1 = "1" Or "2" Or "3" Or "4" Or "5" Or "6" Or "7" Or "7" Or "8" Or "9" Or "0" Then
	 TextBox1.Text = "1"
 Else
	 num1 = "1"
 End If
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
 If sinal = "+" Then
	 TextBox1.Text = num1 + TextBox1.Text
 ElseIf sinal = "-" Then
	 TextBox1.Text = num1 - TextBox1.Text
 ElseIf sinal = "/" Then
	 TextBox1.Text = num1 / TextBox1.Text
 ElseIf sinal = "*" Then
	 TextBox1.Text = num1 * TextBox1.Text
 End If

End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
 If num1 = "1" Or "2" Or "3" Or "4" Or "5" Or "6" Or "7" Or "7" Or "8" Or "9" Or "0" Then
	 TextBox1.Text = "9"
 Else
	 num1 = "9"
 End If
End Sub
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
 If num1 = "1" Or "2" Or "3" Or "4" Or "5" Or "6" Or "7" Or "7" Or "8" Or "9" Or "0" Then
	 TextBox1.Text = "4"
 Else
	 num1 = "4"
 End If
End Sub
Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
 If num1 = "1" Or "2" Or "3" Or "4" Or "5" Or "6" Or "7" Or "7" Or "8" Or "9" Or "0" Then
	 TextBox1.Text = "5"
 Else
	 num1 = "5"
 End If
End Sub
Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
 If num1 = "1" Or "2" Or "3" Or "4" Or "5" Or "6" Or "7" Or "7" Or "8" Or "9" Or "0" Then
	 TextBox1.Text = "2"
 Else
	 num1 = "2"
 End If
End Sub
Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click
 If num1 = "1" Or "2" Or "3" Or "4" Or "5" Or "6" Or "7" Or "7" Or "8" Or "9" Or "0" Then
	 TextBox1.Text = "3"
 Else
	 num1 = "3"
 End If
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
 TextBox1.Text = "-"
 sinal = TextBox1.Text
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
 TextBox1.Text = "/"
 sinal = TextBox1.Text
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
 TextBox1.Text = "*"
 sinal = TextBox1.Text
End Sub
End Class
Link to comment
Share on other sites

olha amigo..

neste print tens a interface: http://prntscr.com/9nhvfq

aqui tens o codigo de um programinha que fiz em vb em 2005

Dim Op1 As Double		   ' Primeiro operando.
Dim Op2 As Double		   ' Segundo operando.
Dim FlagDecimal As Integer  ' Flag do Ponto Decimal.
Dim NumOps As Integer	   ' Numero de Operandos.
Dim UltimaEntrada As String ' Indica a ultima tecla pressionada.
Dim FlagOperacao As String  ' Indica a operacao pendente
Const VERDADEIRO = -1
Const FALSO = 0

' Procedure para a tecla C (Cancela).
' Reseta o display e inicializa variaveis.
'
Private Sub Cancel_Click()
 Number(0).SetFocus 'Volta o Controle para matriz de Numeros
 Visor.Caption = "0."
 Form_Load
End Sub

' Procedure para a tecla CE (Cancela Entrada).
'
Private Sub CancelEntry_Click()
 Number(0).SetFocus 'Volta o Controle para matriz de Numeros
 Visor.Caption = "0."
 FlagDecimal = FALSO
 UltimaEntrada = "CE"
End Sub

' Procedure para a tecla de ponto decimal (.) .
' Se a ultima tecla pressiona fou operador, initializa
' Visor com "0." Senao, adiciona um ponto decimal no display.
'
Private Sub Decimal_Click()
 If UltimaEntrada <> "NUMS" Then
Visor.Caption = "0."
 ElseIf FlagDecimal = FALSO Then
Visor.Caption = Visor.Caption + "."
 End If

 FlagDecimal = VERDADEIRO
 UltimaEntrada = "NUMS"
End Sub

' Rotina de Inicializacao para o formulario
' Inicia todas as variaveis
'
Private Sub Form_Load()
 CENTRALIZA_FORM Me
 FlagDecimal = FALSO
 NumOps = 0
 UltimaEntrada = "NONE"
 FlagOperacao = " "
End Sub

Private Sub mnuSair_Click()
 End
End Sub

' Procedure para as teclas de numeros (0-9).
' Adiciona o novo numero ao numero do display.
'
Private Sub Number_Click(Index As Integer)
 If UltimaEntrada <> "NUMS" Then
Visor.Caption = ""
FlagDecimal = FALSO
 End If
 If Len(Visor.Caption) <= 15 Then 'Limita entrada valores a 15 digitos(inclusive pto decimal)
 Visor.Caption = Visor.Caption + Number(Index).Caption
 End If
 UltimaEntrada = "NUMS"
 Operator(4).SetFocus 'Posiciona o Foco na operação de igual
End Sub

Private Sub Number_KeyPress(Index As Integer, KeyAscii As Integer)
'*** FAZ LEITURA DE TECLAS PRESSIONADAS ***
 If KeyAscii = 61 Then
 Operator_Click (4) 'Sinal de Igual
 ElseIf KeyAscii = 67 Or KeyAscii = 99 Then
 Cancel_Click 'Botao de Limpar
 ElseIf KeyAscii = 37 Then
 Percent_Click 'Sinal de Porcentagem
 ElseIf KeyAscii = 42 Or KeyAscii = 120 Or KeyAscii = 88 Then
 Operator_Click (2) 'Sinal de Multiplicação
 ElseIf KeyAscii = 43 Then
 Operator_Click (1) 'Sinal de Mais
 ElseIf KeyAscii = 45 Then
 Operator_Click (3) 'Sinal de Igual
 ElseIf KeyAscii = 46 Then
 Decimal_Click	  'Ponto decimal
 ElseIf KeyAscii = 47 Then
 Operator_Click (0) 'Sinal de Divisao
 ElseIf KeyAscii = 48 Then
 Number_Click (0)
 ElseIf KeyAscii = 49 Then
 Number_Click (1)
 ElseIf KeyAscii = 50 Then
 Number_Click (2)
 ElseIf KeyAscii = 51 Then
 Number_Click (3)
 ElseIf KeyAscii = 52 Then
 Number_Click (4)
 ElseIf KeyAscii = 53 Then
Number_Click (5)
 ElseIf KeyAscii = 54 Then
Number_Click (6)
 ElseIf KeyAscii = 55 Then
Number_Click (7)
 ElseIf KeyAscii = 56 Then
Number_Click (8)
 ElseIf KeyAscii = 57 Then
Number_Click (9)
 End If


End Sub

' Procedure para os teclas de operadores (+, -, x, /, =).
' Se a tecla pressiona imediatamente foi parte de um
' numero, incrementa a variavel NumOps. Se um operando esta presente,
' "seta" Op1. Se dois operandos estao presentes, "seta" Op1 igual ao
' resultado da operacao em Op1 com a string entrada por ultimo e mostra
' o resultado.
'
Private Sub Operator_Click(Index As Integer)
Operator(4).SetFocus   'Volta o Controle para matriz de Numeros
 If UltimaEntrada = "NUMS" Then
NumOps = NumOps + 1
 End If
 If NumOps = 1 Then
Op1 = Val(Visor.Caption)
 ElseIf NumOps = 2 Then
Op2 = Val(Visor.Caption)
 Select Case FlagOperacao
Case "+"
 Op1 = Op1 + Op2
Case "-"
 Op1 = Op1 - Op2
Case "X"
 Op1 = Op1 * Op2
Case "/"
If Op2 = 0 Then
  MsgBox "Erro, Divisão por zero impossível", 48, "Spasso Calc"
Else
  Op1 = Op1 / Op2
End If
Case "="
 Op1 = Op2
 End Select
 Visor.Caption = Format$(Op1)
NumOps = 1
End If

UltimaEntrada = "OPS"
FlagOperacao = Operator(Index).Caption
End Sub

Private Sub Operator_KeyPress(Index As Integer, KeyAscii As Integer)
'*** FAZ LEITURA DE TECLAS PRESSIONADAS ***
If KeyAscii = 61 Then
 Operator_Click (4) 'Sinal de Igual
ElseIf KeyAscii = 67 Or KeyAscii = 99 Then
 Cancel_Click	  'Botao de Limpar
ElseIf KeyAscii = 37 Then
 Percent_Click	 'Sinal de Porcentagem
ElseIf KeyAscii = 42 Or KeyAscii = 120 Or KeyAscii = 88 Then
 Operator_Click (2) 'Sinal de Multiplicação
ElseIf KeyAscii = 43 Then
 Operator_Click (1) 'Sinal de Mais
ElseIf KeyAscii = 45 Then
 Operator_Click (3) 'Sinal de Igual
ElseIf KeyAscii = 46 Then
 Decimal_Click 'Ponto decimal
ElseIf KeyAscii = 47 Then
 Operator_Click (0) 'Sinal de Divisao
ElseIf KeyAscii = 48 Then
 Number_Click (0)
ElseIf KeyAscii = 49 Then  
 Number_Click (1)
ElseIf KeyAscii = 50 Then
 Number_Click (2)
ElseIf KeyAscii = 51 Then
 Number_Click (3)
ElseIf KeyAscii = 52 Then
 Number_Click (4)
ElseIf KeyAscii = 53 Then
 Number_Click (5)
ElseIf KeyAscii = 54 Then
 Number_Click (6)
ElseIf KeyAscii = 55 Then
 Number_Click (7)
ElseIf KeyAscii = 56 Then
 Number_Click (8)
ElseIf KeyAscii = 57 Then
 Number_Click (9)
End If

End Sub

' Procedure para a tecla de percentagem (%).
' Computa and mostra a percentagem do primeiro operando.
'
Private Sub Percent_Click()
  Visor.Caption = Format$(Op1 * Val(Visor.Caption) / 100)
End Sub

Public Sub CENTRALIZA_FORM(Formulario As Form)
On Error Resume Next  'Evita erro caso o usuário minimize o Form
With Formulario
 .Left = (Screen.Width - .Width) / 2 'Alinha o form no horizontalmente no centro
 .Top = (Screen.Height - .Height) / 2 'Alinha o form no verticalmente no centro
End With
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.