VB calc code need help on last portions

jmoney3457jmoney3457 Maine
edited November 2007 in Internet & Media
here is my code:
Public Class Form1
Dim first As Double
Dim second As Double
Dim sign As String

Private Sub btnzero_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnzero.Click

txtdisplay.Text = btnzero.Text
'this allows for 0 to enter textbox when clicked
End Sub

Private Sub btnone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnone.Click
txtdisplay.Text = btnone.Text
'this allows for 1 to enter textbox when clicked
End Sub

Private Sub btntwo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntwo.Click
txtdisplay.Text = btntwo.Text
'this allows for 2 to enter textbox when clicked
End Sub

Private Sub btnthree_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthree.Click
txtdisplay.Text = btnthree.Text
'this allows for 3 to enter textbox when clicked
End Sub

Private Sub btnfour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfour.Click
txtdisplay.Text = btnfour.Text
'this allows for 4 to enter textbox when clicked
End Sub

Private Sub btnfive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfive.Click
txtdisplay.Text = btnfive.Text
'this allows for 5 to enter textbox when clicked
End Sub

Private Sub btnsix_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsix.Click
txtdisplay.Text = btnsix.Text
'this allows for 6 to enter textbox when clicked
End Sub

Private Sub btnseven_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnseven.Click
txtdisplay.Text = btnseven.Text
'this allows for 7 to enter textbox when clicked
End Sub

Private Sub btneight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btneight.Click
txtdisplay.Text = btneight.Text
'this allows for 8 to enter textbox when clicked
End Sub

Private Sub btnnine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnine.Click
txtdisplay.Text = btnnine.Text
'this allows for 9 to enter textbox when clicked
End Sub

Private Sub cmdclear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdclear.Click
txtdisplay.Text = ""
End Sub
End Class

so far i've gotten it so when you press the digits 0-9 they will display in the textbox (txtdisplay) but now how do i get the operators (commands) to function and get the = command to display the answer in the textbox? also i need some if..else statements where would i put those in the code/how would i use them?

Comments

  • Park_7677Park_7677 Missouri Member
    edited November 2007
    Here's a very simple calculator code in VB.NET I just wrote. It does addition, subtraction, multiplication, and division of two numbers.
    Public Class Form1
    
        Dim first As Double
        Dim second As Double
        Dim sign As String
        Dim intStep As Integer
    
        Private Sub cmdNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNum0.Click, cmdNum1.Click, cmdNum2.Click, cmdNum3.Click, cmdNum4.Click, cmdNum5.Click, cmdNum6.Click, cmdNum7.Click, cmdNum8.Click, cmdNum9.Click
            ' If the textbox has a 0 in it, erase it for number entry:
            If txtDisplay.Text = "0." Or txtDisplay.Text = "0" Then txtDisplay.Text = Nothing
    
            ' Set the text equal to the current text and add the value of the button clicked to the end:
            txtDisplay.Text = txtDisplay.Text & CType(sender, Button).Text
        End Sub
    
        Private Sub cmdFunc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFuncDivide.Click, cmdFuncMultiply.Click, cmdFuncSubtract.Click, cmdFuncAddition.Click
            ' Set the function sign based on the text of the button clicked:
            sign = CType(sender, Button).Text
    
            ' Set the first number equal to the display text:
            first = txtDisplay.Text
    
            ' Clear the text for the second number entry:
            txtDisplay.Text = "0."
    
            ' Set the flag to tell the program it can calculate:
            intStep = 1
        End Sub
    
        Private Sub doCalculation()
            ' If a first number has already been entered we can calculate:
            If intStep = 1 Then
                ' Set the second number equal to the current text:
                second = txtDisplay.Text
    
                ' Find out what sign is equal to in order to do the calculation:
                Select Case sign
                    Case "+"
                        ' Addition:
                        txtDisplay.Text = first + second
                    Case "-"
                        ' Subtraction:
                        txtDisplay.Text = first - second
                    Case "*"
                        ' Multiplication:
                        txtDisplay.Text = first * second
                    Case "/"
                        ' Division:
                        txtDisplay.Text = first / second
                End Select
                ' Reset the flag to say we can't yet calculate:
                intStep = 0
                first = 0
                second = 0
                sign = ""
            End If
        End Sub
    
        Private Sub cmdFuncEqual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFuncEqual.Click
            ' Call the sub to calculate:
            doCalculation()
        End Sub
    
        Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click
            ' Clear the first and second numbers as well as the sign:
            first = 0
            second = 0
            sign = ""
            txtDisplay.Text = "0."
        End Sub
    
        Private Sub cmdDecimal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDecimal.Click
            ' Check if we already have a decimal sign
            If txtDisplay.Text.IndexOf(".") = -1 Then
                ' No previous decimal found so add one to the end:
                txtDisplay.Text = txtDisplay.Text & "."
            End If
        End Sub
    
        Private Sub cmdChangeSign_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdChangeSign.Click
            ' Check to see if our number begins with negative sign:
            If txtDisplay.Text.StartsWith("-") = False Then
                ' No negative found so it is currently positive. Change it:
                txtDisplay.Text = "-" & txtDisplay.Text
            Else
                ' There is a negative sign found so remove it:
                txtDisplay.Text = txtDisplay.Text.Replace("-", "")
            End If
        End Sub
    End Class
    
    Take anything that you may need from it. If you have questions go ahead and ask.
  • jmoney3457jmoney3457 Maine
    edited November 2007
    thx alot park:) appreciate it man..i think i've got it from here but ill ask you for anything in the future about VB:D
Sign In or Register to comment.