Sie sind auf Seite 1von 5

Calculadora

tente esse, eh s abrir o VB e colar esse codigo num novo form. : - Inicie um novo projeto no VB e mude o nome do formulrio padro para frmcalculadora. - Insira uma caixa de texto e alguns botes de comando conforme layout abaixo:

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 operao 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 Multiplicao 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, Diviso por zero impossvel", 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 Multiplicao 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 usurio 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

Das könnte Ihnen auch gefallen