Jump to content
gui09

Calculadora Visual Basic

Recommended Posts

gui09

Olá a todos. Eu estou a fazer uma calculadora em vb parecida com a do windows, até agora consigo fazer as operações básicas, raiz quadrada e por qualquer número com um expoente. Eu queria agora era fazer com que a calculadora fizesse por exemplo 1+2+3 de seguida e não estar sempre a fazer 1+2=3 e depois somar ao resultado + 3.

Este é o código que tenho. Agradeço toda a ajuda que puderem dar.

Public Class Form1

    Dim val1, val2 As Decimal
    Dim operacao As String
    Dim ativo As Integer
    Dim igual As Integer = 0


    Private Sub numb1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numb1.Click

        If Me.TextBox1.Text = "0" Or ativo = 1 Then
            Me.TextBox1.Text = "1"
            ativo = 0
        Else
            Me.TextBox1.Text = Me.TextBox1.Text & "1"

        End If

    End Sub

    Private Sub numb2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numb2.Click

        If Me.TextBox1.Text = "0" Or ativo = 1 Then
            Me.TextBox1.Text = "2"
            ativo = 0
        Else
            Me.TextBox1.Text = Me.TextBox1.Text & "2"

        End If

    End Sub

    Private Sub numb3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numb3.Click

        If Me.TextBox1.Text = "0" Or ativo = 1 Then
            Me.TextBox1.Text = "3"
            ativo = 0
        Else
            Me.TextBox1.Text = Me.TextBox1.Text & "3"

        End If

    End Sub

    Private Sub numb4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numb4.Click

        If Me.TextBox1.Text = "0" Or ativo = 1 Then
            Me.TextBox1.Text = "4"
            ativo = 0
        Else
            Me.TextBox1.Text = Me.TextBox1.Text & "4"

        End If

    End Sub

    Private Sub numb5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numb5.Click

        If Me.TextBox1.Text = "0" Or ativo = 1 Then
            Me.TextBox1.Text = "5"
            ativo = 0
        Else
            Me.TextBox1.Text = Me.TextBox1.Text & "5"

        End If

    End Sub

    Private Sub numb6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numb6.Click

        If Me.TextBox1.Text = "0" Or ativo = 1 Then
            Me.TextBox1.Text = "6"
            ativo = 0
        Else
            Me.TextBox1.Text = Me.TextBox1.Text & "6"

        End If

    End Sub

    Private Sub numb7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numb7.Click

        If Me.TextBox1.Text = "0" Or ativo = 1 Then
            Me.TextBox1.Text = "7"
            ativo = 0
        Else
            Me.TextBox1.Text = Me.TextBox1.Text & "7"

        End If

    End Sub

    Private Sub numb8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numb8.Click

        If Me.TextBox1.Text = "0" Or ativo = 1 Then
            Me.TextBox1.Text = "8"
            ativo = 0
        Else
            Me.TextBox1.Text = Me.TextBox1.Text & "8"

        End If

    End Sub

    Private Sub numb9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numb9.Click

        If Me.TextBox1.Text = "0" Or ativo = 1 Then
            Me.TextBox1.Text = "9"
            ativo = 0
        Else
            Me.TextBox1.Text = Me.TextBox1.Text & "9"

        End If

    End Sub

    Private Sub numb0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numb0.Click

        If Me.TextBox1.Text <> "0" Or ativo = 1 Then
            Me.TextBox1.Text = Me.TextBox1.Text & "0"
            ativo = 0

        End If

    End Sub

    Private Sub comma_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comma.Click

        Me.TextBox1.Text = Me.TextBox1.Text & ","

    End Sub

    Private Sub clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clear.Click

        Me.TextBox1.Text = "0"
        val1 = 0
        val2 = 0



    End Sub

    Private Sub add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles add.Click

        val1 = Me.TextBox1.Text
        ativo = 1
        operacao = "+"


    End Sub

    Private Sub equalizer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles equalizer.Click

        Dim resultado As Decimal
        If igual = 0 Then
            val2 = Me.TextBox1.Text
        End If


        If operacao = "+" Then
            resultado = val1 + val2
            Me.TextBox1.Text = resultado



        ElseIf operacao = "-" Then
            resultado = val1 - val2
            Me.TextBox1.Text = resultado



        ElseIf operacao = "*" Then
            resultado = val1 * val2
            Me.TextBox1.Text = resultado



        ElseIf operacao = "/" Then
            resultado = val1 / val2
            Me.TextBox1.Text = resultado

        ElseIf operacao = "exp" Then
            resultado = Math.Pow(val1, val2)
            Me.TextBox1.Text = resultado


        End If

        ativo = 1

        igual = 0
        val1 = resultado

    End Sub

    Private Sub subtracao_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles subtracao.Click

        val1 = Me.TextBox1.Text
        ativo = 1
        operacao = "-"


    End Sub

    Private Sub mult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mult.Click

        val1 = Me.TextBox1.Text
        ativo = 1
        operacao = "*"


    End Sub

    Private Sub divide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles divide.Click

        val1 = Me.TextBox1.Text
        ativo = 1
        operacao = "/"


    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If Me.TextBox1.Text.Length >= 20 Then
            Me.TextBox1.Text = Me.TextBox1.Text.Substring(0, 20)
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ativo = 0

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.TextBox1.Text = Math.Sqrt(Me.TextBox1.Text)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles expoentebottom.Click
        val1 = Me.TextBox1.Text
        ativo = 1
        operacao = "exp"

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    End Sub
End Class

Share this post


Link to post
Share on other sites
thoga31

Quando clicas num operador, manda fazer a operação automaticamente. Do tipo, fazes "1" e depois "+", ora ele faz, por defeito 0+1. Depois fazes "2" e de novo "+", o que dará 1+2 e aparecerá "3" na textbox. Fazes "3" e "=" e fará 3+3.

Ou seja, cada operador será, ao mesmo tempo, o "=" se tal fizer sentido.


Knowledge is free!

Share this post


Link to post
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

×
×
  • 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.