3 Replies - 890 Views - Last Post: 22 December 2013 - 12:04 AM Rate Topic: -----

#1 enigma111  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-March 12

Need help with a simple calculator I am writing.

Posted 15 December 2013 - 09:15 PM

I am creating a calculator for my vb class. The program is near completion, but I hit a stonewall with the decimal button. Basically, I want to ensure that only one decimal can be used in each number.

Here is the part of my code that is broken...
 Private Sub btnDec_Click(sender As Object, e As EventArgs) Handles btnDec.Click
        Dim DecPresentBool As Boolean
        If DecPresentBool = False Then
            txtBox1.Text = txtBox1.Text & "."
            DecPresentBool = True
        End If
    End Sub



And here is my code in it's entirety...

 
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles txtBox1.TextChanged

    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        txtBox1.Clear() 'Clears the txtbox1.
        lbl1.Text = "" 'Clears lbl1.
        lbl2.Text = "" 'Clears lbl2.
        lbl3.Text = "" 'Clears lbl3.

    End Sub

    Private Sub btnSqrt_Click(sender As Object, e As EventArgs) Handles btnSqrt.Click
        
        Dim sqrt As Double 'delcares sqrt As Double.
        Try 'Informs the user that their input is invalid, when they press the sqrt button before enterering a number.
            sqrt = Convert.ToDouble(txtBox1.Text)
            txtBox1.Text = Convert.ToString(Math.Sqrt(sqrt))
        Catch ex As Exception
            MsgBox("Invalid Input! Enter number first.")
        End Try

    End Sub

    Private Sub btnNegPos_Click(sender As Object, e As EventArgs) Handles btnNegPos.Click
        Dim numb As Double 'Declares variable numb as Double. 
        Try 'Informs the user that their input is invalid, when they press the -+ button before enterering a number.
            numb = Convert.ToDouble(txtBox1.Text)
            Dim nu As Double 'Declares variable nu as Double. 
            nu = numb - numb - numb
            txtBox1.Text = Convert.ToString(nu)  'value of nu will print to txtbox1.
        Catch ex As Exception
            MsgBox("Invalid Input! Enter number first.")
        End Try
    End Sub

    Private Sub btn0_Click(sender As Object, e As EventArgs) Handles btn0.Click
        txtBox1.AppendText(0) 'Print a 0 in txtbox1.
    End Sub

    Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
        txtBox1.AppendText(1) 'Print a 1 in txtbox1.
    End Sub

    Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
        txtBox1.AppendText(2) 'Print a 2 in txtbox1.
    End Sub

    Private Sub btn3_Click(sender As Object, e As EventArgs) Handles btn3.Click
        txtBox1.AppendText(3) 'Print a 3 in txtbox1.
    End Sub

    Private Sub btn4_Click(sender As Object, e As EventArgs) Handles btn4.Click
        txtBox1.AppendText(4) 'Print a 4 in txtbox1.
    End Sub

    Private Sub btn5_Click(sender As Object, e As EventArgs) Handles btn5.Click
        txtBox1.AppendText(5) 'Print a 5 in txtbox1.
    End Sub

    Private Sub btn6_Click(sender As Object, e As EventArgs) Handles btn6.Click
        txtBox1.AppendText(6) 'Print a 6 in txtbox1.
    End Sub

    Private Sub btn7_Click(sender As Object, e As EventArgs) Handles btn7.Click
        txtBox1.AppendText(7) 'Print a 7 in txtbox1.
    End Sub

    Private Sub btn8_Click(sender As Object, e As EventArgs) Handles btn8.Click
        txtBox1.AppendText(8) 'Print a 8 in txtbox1.
    End Sub

    Private Sub btn9_Click(sender As Object, e As EventArgs) Handles btn9.Click
        txtBox1.AppendText(9) 'Print a 9 in txtbox1.
    End Sub

    Private Sub btnDec_Click(sender As Object, e As EventArgs) Handles btnDec.Click
        Dim DecPresentBool As Boolean
        If DecPresentBool = False Then
            txtBox1.Text = txtBox1.Text & "."
            DecPresentBool = True
        End If
    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        lbl1.Text = txtBox1.Text
        lbl2.Text = "+"
        txtBox1.Clear()
    End Sub

    Private Sub btnSub_Click(sender As Object, e As EventArgs) Handles btnSub.Click
        lbl1.Text = txtBox1.Text
        lbl2.Text = "-"
        txtBox1.Clear()
    End Sub

    Private Sub btnMult_Click(sender As Object, e As EventArgs) Handles btnMult.Click
        lbl1.Text = txtBox1.Text
        lbl2.Text = "*"
        txtBox1.Clear()
    End Sub

    Private Sub btnDivide_Click(sender As Object, e As EventArgs) Handles btnDivide.Click
        lbl1.Text = txtBox1.Text
        lbl2.Text = "/"
        txtBox1.Clear()
    End Sub

    Private Sub btnEqu_Click(sender As Object, e As EventArgs) Handles btnEqu.Click
        lbl3.Text = txtBox1.Text 'Prints the answer stored in lbl3 in txtbox1.
        Dim sign As Char 'Declares variable sign as Char.
        sign = lbl2.Text 'Prints whatever sign the user selects in lbl2.
        Dim n1 As Double 'Declares variable n1 as Double.
        n1 = Convert.ToDouble(lbl1.Text) 'n1 is stored in lbl1.
        Dim n2 As Double 'Declares variable n2 As Double.
        n2 = Convert.ToDouble(lbl3.Text) 'n2 is stored in lbl3.
        Dim n3 As Double  'Declares n3 as Double.

        'Select Case for addition, subtraction, multiplication, and divivision.
        Select Case (sign)
            Case "+"
                n3 = n1 + n2
            Case "-"
                n3 = n1 - n2
            Case "*"
                n3 = n1 * n2
            Case "/"
                n3 = n1 / n2

                'Ensures that the user cannot divide by zero.
                If n2 = "0" Then
                    'message displayed if trying to divide by zero.
                    MsgBox("You Cannot Divide by Zero")
                End If
        End Select
        txtBox1.Text = Convert.ToString(n3)


    End Sub
End Class



Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with a simple calculator I am writing.

#2 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 299
  • View blog
  • Posts: 1,755
  • Joined: 26-March 09

Re: Need help with a simple calculator I am writing.

Posted 16 December 2013 - 02:20 AM

This looks like VB.Net code, so technically in the wrong forum.

That being said, look at the scope of your Boolean DecPresentBool.

You declare it in the button click handler, so it's scope is only in that handler, so it's declared every time you click the button.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2875
  • View blog
  • Posts: 9,538
  • Joined: 12-December 12

Re: Need help with a simple calculator I am writing.

Posted 16 December 2013 - 05:44 AM

Moved to VB.NET.
Was This Post Helpful? 0
  • +
  • -

#4 Metitron  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 78
  • Joined: 13-October 09

Re: Need help with a simple calculator I am writing.

Posted 22 December 2013 - 12:04 AM

ANother way would to be to check the contents of the text box when the button is clicked

If textbox1.text.contains(".") Then
do nothing
else
textbox1.text += "."
End IF

The reason I say this is because global variable can become problematic in large programs and its just esier to get away from using them now rather than trying to later.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1