Hello, im making a calculator for a school project with visual basic using microsoft visual studio 2010.
I´m using a textbox and a label to calculate my results but I´d rather have everything in 1 textbox, or richtextbox. I searched on the internet and I found I could use microsoft script control 1.0 to avoid complications. But because I never learnt how to script in visual basic it´s pretty hard for me so i wondered if you guys could help me out.
Here´s my code so far:

Public Class Form1

    Private Sub Button0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button0.Click
        TextBox1.Text += "0"
    End Sub

    Private Sub Buttondot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttondot.Click
        Dim dot As Boolean = False
        If TextBox1.Text.IndexOf(".") >= 0 Then dot = True
        If dot = False Then TextBox1.Text += "."
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text += "1"
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Text += "2"
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        TextBox1.Text += "3"
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        TextBox1.Text += "4"
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        TextBox1.Text += "5"
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        TextBox1.Text += "6"
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        TextBox1.Text += "7"
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        TextBox1.Text += "8"
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        TextBox1.Text += "9"
    End Sub

    Private Sub Buttondivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttondivide.Click
        Label1.Text = Val(TextBox1.Text)
        Label2.Text = "/"
        TextBox1.Text = ""
    End Sub

    Private Sub Buttonmultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonmultiply.Click
        Label1.Text = Val(TextBox1.Text)
        Label2.Text = "×"
        TextBox1.Text = ""
    End Sub

    Private Sub Buttonminus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonminus.Click
        Label1.Text = Val(TextBox1.Text)
        Label2.Text = "-"
        TextBox1.Text = ""
    End Sub

    Private Sub Buttonplus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonplus.Click
        Label1.Text = Val(TextBox1.Text)
        Label2.Text = "+"
        TextBox1.Text = ""
    End Sub

    Private Sub Buttonequals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonequals.Click
        Dim dot As Boolean = False
        If Label1.Text.IndexOf(".") >= 0 Then dot = True
        If Label2.Text = "/" Then
            TextBox1.Text = Val(Label1.Text) / Val(TextBox1.Text)
        End If
        If Label2.Text = "×" Then
            TextBox1.Text = Val(Label1.Text) * Val(TextBox1.Text)
        End If
        If Label2.Text = "-" Then
            TextBox1.Text = Val(Label1.Text) - Val(TextBox1.Text)
        End If
        If Label2.Text = "+" Then
            TextBox1.Text = Val(Label1.Text) + Val(TextBox1.Text)
        End If
        Label1.Text = ""
        Label2.Text = ""
    End Sub

    Private Sub ButtonClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonClear.Click
        TextBox1.Text = ""
        Label1.Text = ""
        Label2.Text = ""
    End Sub

End Class

Thanks in advance!
Btw: my dot for fractions is not working very well either.

I have ask that your post be moved to .Net. I'm sure someone over there will help. This is unfortunately vb4/5/6 forum.:)

here is a good working code block:

Imports System.Math

Public Class calculator

    Dim cleardisplay As Boolean
    Dim operand1 As Double
    Dim operand2 As Double
    Dim Operator1 As String

    Private Sub Digit_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btn1.Click, btn2.Click, _
            btn3.Click, btn4.Click, btn5.Click, btn6.Click, _
            btn7.Click, btn8.Click, btn9.Click, btn0.Click
        If cleardisplay Then
            txtDisplay.Text = ""
            cleardisplay = False
        End If
        txtDisplay.Text = txtDisplay.Text + sender.Text
    End Sub

    Private Sub btnPeriod_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPeriod.Click
        If txtDisplay.Text.IndexOf(".") > 0 Then
            Exit Sub
        Else
            txtDisplay.Text = txtDisplay.Text & "."
        End If
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtDisplay.Text = ""
    End Sub

    Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click

        Dim result As Double
        operand2 = Val(txtDisplay.Text)
        Select Case Operator1
            Case "+"
                result = operand1 + operand2
            Case "-"
                result = operand1 - operand2
            Case "*"
                result = operand1 * operand2
            Case "/"
                If operand2 <> "0" Then result = operand1 / operand2
        End Select
        txtDisplay.Text = result
        cleardisplay = True
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        operand1 = Val(txtDisplay.Text)
        Operator1 = "+"
        cleardisplay = True
    End Sub

    Private Sub btnPrefix_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrefix.Click
        txtDisplay.Text = -Val(txtDisplay.Text)
        cleardisplay = True
    End Sub

    Private Sub btnInvers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInverse.Click
        If Val(txtDisplay.Text) <> 0 Then txtDisplay.Text = 1 / Val(txtDisplay.Text)
        cleardisplay = True
    End Sub

    Private Sub btnTimes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimes.Click
        operand1 = Val(txtDisplay.Text)
        Operator1 = "*"
        cleardisplay = True
    End Sub

    Private Sub btnMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMinus.Click
        operand1 = Val(txtDisplay.Text)
        Operator1 = "-"
        cleardisplay = True
    End Sub

    Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
        operand1 = Val(txtDisplay.Text)
        Operator1 = "/"
        cleardisplay = True
    End Sub

    Private Sub btnLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLog.Click
        If Val(txtDisplay.Text) < 0 Then
            MsgBox("Can’t calculate the logarithm of a negative number")
        Else
            txtDisplay.Text = Math.Log(txtDisplay.Text)
        End If
        cleardisplay = True
    End Sub

    Private Sub btnCos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCos.Click
        txtDisplay.Text = Math.Cos(Val(txtDisplay.Text))
    End Sub

    Private Sub btnSine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSine.Click
        txtDisplay.Text = Math.Sin(Val(txtDisplay.Text))
    End Sub

    Private Sub btnTan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTan.Click
        txtDisplay.Text = Math.Tan(Val(txtDisplay.Text))
    End Sub

    Private Sub btnSquareRoot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSquareRoot.Click
        txtDisplay.Text = Math.Sqrt(Val(txtDisplay.Text))
    End Sub

    Private Sub btnSquare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSquare.Click
        operand1 = Val(txtDisplay.Text) * Val(txtDisplay.Text)
        txtDisplay.Text = operand1
        cleardisplay = True
    End Sub


    Private Sub btnRoot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRoot.Click
        
    End Sub

    Private Sub btnAbsolute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbsolute.Click
        txtDisplay.Text = Math.Abs(Val(txtDisplay.Text))
    End Sub

    Private Sub txtDisplay_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDisplay.TextChanged
        If Not IsNumeric(txtDisplay.Text) Then
            txtDisplay.Text = ""
        End If
    End Sub

    Private Sub btnExponent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExponent.Click
        txtDisplay.Text = Math.Exp(Val(txtDisplay.Text))
    End Sub

    Private Sub btnLn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLn.Click
        txtDisplay.Text = Math.Log10(Val(txtDisplay.Text))
    End Sub
Comments
perfect for me
Nice post!

thanks! this really helped me out:)

now i was wondering, would it be possible to see everything on the display, so it would give: 8×(7+6) = 104 for example, all on the display.

you can save this as a string and then on = break it down and do the calculations. be careful to keep the correct hierarchy of calculations or you results will not make any sence.


ps: i thought this is a vb.net forum

then why you posted the following

1 Day Ago
This post is useful and well-written 0 This post is unclear
Re: vb calculator
I have ask that your post be moved to .Net. I'm sure someone over there will help. This is unfortunately vb4/5/6 forum.

@ mariandi
how do i break down a string for doing calculations?

i suggest the .contain() and .split() functionality.

if you_string.contains("+") then
     dim newArraw() as String

     newArray =you_string.split("+")
     
     dim sum as decimal
     sum = CDecimal(newArray(0).remove("+")) +CDecimal( newArray(1))

.....................................


I have not tested this code so it could have some syntax errors in it.
it is just to help you get started.

the logic is like this:
first find the parenthesis -()
get what in them and do the calculation and save it in a temp variable
then find what is outside the parenthesis and do the calculation with the result you got from the parenthesi.

careful to always include the change of not having parenthesis or a specific calculation like - or *

Edited 6 Years Ago by __avd: Added [code] tags.

no sorry i hadn't had the time to test it yet, but i will try it asap, although im quite busy atm :)

i dont know whether ive done it right, but the outcome is 0 every time

How about if there is only two text box ,one for the problem example 1+2*3 and another for the answer.and a calculate button?

for (int i=0;i<=10;i++)
system.out.printn("the worled becomes hot for"+i+"year")

thanks! this really helped me out:)

now i was wondering, would it be possible to see everything on the display, so it would give: 8×(7+6) = 104 for example, all on the display.

Well you can try this out I think it will answer your question
This is the full code but it just a standard calculator for you to advance this you just need to add some more tricks or methods of doing things.

Public Class Form1
    Dim numb1, numb2 As Integer
    Dim sign As String


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = TextBox1.Text & "1"
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Text = TextBox1.Text & "2"
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        TextBox1.Text = TextBox1.Text & "3"
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        TextBox1.Text = TextBox1.Text & "4"
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        TextBox1.Text = TextBox1.Text & "5"
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        TextBox1.Text = TextBox1.Text & "6"
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        TextBox1.Text = TextBox1.Text & "7"
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        TextBox1.Text = TextBox1.Text & "8"
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        TextBox1.Text = TextBox1.Text & "9"
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        TextBox1.Text = TextBox1.Text & "0"
    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        numb1 = TextBox1.Text
        TextBox1.Text = ""
        sign = "+"
    End Sub

    Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
        numb2 = TextBox1.Text
        Dim ans As Decimal
        If sign = "+" Then
            ans = numb1 + numb2
        End If
        If sign = "-" Then
            ans = numb1 - numb2
        End If
        If sign = "*" Then
            ans = numb1 * numb2
        End If
        If sign = "/" Then
            ans = numb1 / numb2
        End If
        TextBox1.Text = numb1 & sign & numb2 & " " & "=" & " " & ans

    End Sub

    Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
        numb1 = TextBox1.Text
        TextBox1.Text = ""
        sign = "-"
    End Sub

    Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
        numb1 = TextBox1.Text
        TextBox1.Text = ""
        sign = "*"
    End Sub

    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
        numb1 = TextBox1.Text
        TextBox1.Text = ""
        sign = "/"
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MaximizeBox = False
    End Sub
End Class
Comments
do you really think that that person is still waiting for an answer after 2 years has gone by?
This article has been dead for over six months. Start a new discussion instead.