1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

.vb calculator code

Discussion in 'Software Development' started by dragonempres, Oct 6, 2003.

Thread Status:
Not open for further replies.
Advertisement
  1. dragonempres

    dragonempres Thread Starter

    Joined:
    Jun 22, 2003
    Messages:
    28
    heys, i have to make a calculator for a school project but it won't work right, can anyone tell me whats wrong with this code?? .....


    Public Class CalculatorForm
    Inherits System.Windows.Forms.Form

    Private mintFirstNum As Integer
    Private mintSecondNum As Integer
    Private mblnAdd As Boolean
    Private mblnSubtract As Boolean
    Private mblnMultiply As Boolean
    Private mblnDivide As Boolean
    Private chaSign As Char

    Windows FormDesigner generated code

    Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
    Me.Close()
    End Sub

    Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
    'prepare scree for next calculation
    Me.DisplayLabel.Text = ""
    End Sub

    Private Sub ZeroButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ZeroButton.Click
    Me.DisplayLabel.Text = Me.DisplayLabel.Text & "0"
    End Sub

    Private Sub OneButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OneButton.Click
    Me.DisplayLabel.Text = Me.DisplayLabel.Text & "1"
    End Sub

    Private Sub TwoButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TwoButton.Click
    Me.DisplayLabel.Text = Me.DisplayLabel.Text & "2"
    End Sub

    Private Sub ThreeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ThreeButton.Click
    Me.DisplayLabel.Text = Me.DisplayLabel.Text & "3"
    End Sub

    Private Sub FourButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FourButton.Click
    Me.DisplayLabel.Text = Me.DisplayLabel.Text & "4"
    End Sub


    Private Sub FiveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FiveButton.Click
    Me.DisplayLabel.Text = Me.DisplayLabel.Text & "5"
    End Sub

    Private Sub SixButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SixButton.Click
    Me.DisplayLabel.Text = Me.DisplayLabel.Text & "6"
    End Sub

    Private Sub SevenButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SevenButton.Click
    Me.DisplayLabel.Text = Me.DisplayLabel.Text & "7"
    End Sub

    Private Sub EightButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EightButton.Click
    Me.DisplayLabel.Text = Me.DisplayLabel.Text & "8"
    End Sub

    Private Sub NineButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NineButton.Click
    Me.DisplayLabel.Text = Me.DisplayLabel.Text & "9"
    End Sub


    Private Sub AddNumButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddNumButton.Click
    'save the firstnumber
    Me.DisplayLabel.Text = mintFirstNum
    'clear the scree so user can enter second number
    Me.DisplayLabel.Text = ""
    'save the sign
    mblnAdd = True
    mblnSubtract = False
    mblnMultiply = False
    mblnDivide = False
    'save second number
    Me.DisplayLabel.Text = mintSecondNum
    End Sub

    Private Sub SubtNumButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubtNumButton.Click
    'save the firstnumber
    Me.DisplayLabel.Text = mintFirstNum
    'clear the scree so user can enter second number
    Me.DisplayLabel.Text = ""
    'save the sign
    mblnAdd = False
    mblnSubtract = True
    mblnMultiply = False
    mblnDivide = False
    'save second number
    Me.DisplayLabel.Text = mintSecondNum
    End Sub

    Private Sub MultNumButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MultNumButton.Click
    'save the firstnumber
    Me.DisplayLabel.Text = mintFirstNum
    'clear the scree so user can enter second number
    Me.DisplayLabel.Text = ""
    'save the sign
    mblnAdd = False
    mblnSubtract = False
    mblnMultiply = True
    mblnDivide = False
    'save second number
    Me.DisplayLabel.Text = mintSecondNum
    End Sub

    Private Sub DivNumButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DivNumButton.Click
    'save the firstnumber
    Me.DisplayLabel.Text = mintFirstNum
    'clear the scree so user can enter second number
    Me.DisplayLabel.Text = ""
    'save the sign
    mblnAdd = False
    mblnSubtract = False
    mblnMultiply = False
    mblnDivide = True
    'save second number
    Me.DisplayLabel.Text = mintSecondNum
    End Sub

    Private Sub EqualsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EqualsButton.Click
    If mblnAdd = True Then Me.DisplayLabel.Text = mintFirstNum + mintSecondNum _
    Else If mblnSubtract = True Then Me.DisplayLabel.Text = mintFirstNum - mintSecondNum _
    Else If mblnMultiply = True Then Me.DisplayLabel.Text = mintFirstNum * mintSecondNum _
    Else If mblnDivide = True Then Me.DisplayLabel.Text = mintFirstNum \ mintSecondNum
    End Sub
    End Class
     
  2. dragonempres

    dragonempres Thread Starter

    Joined:
    Jun 22, 2003
    Messages:
    28
    mkays, i changed it so that it saves the second number when you hit the calc button and that solved one of my problems, but it still wont calculate/display the answer....

    Private Sub EqualsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EqualsButton.Click
    'save second number
    Me.DisplayLabel.Text = mintSecondNum
    'calculate answer
    If mblnAdd = True Then Me.DisplayLabel.Text = mintFirstNum + mintSecondNum _
    Else If mblnSubtract = True Then Me.DisplayLabel.Text = mintFirstNum - mintSecondNum _
    Else If mblnMultiply = True Then Me.DisplayLabel.Text = mintFirstNum * mintSecondNum _
    Else If mblnDivide = True Then Me.DisplayLabel.Text = mintFirstNum \ mintSecondNum
    End Sub
     
  3. dragonempres

    dragonempres Thread Starter

    Joined:
    Jun 22, 2003
    Messages:
    28
    save second number
    Me.DisplayLabel.Text = mintSecondNum
    'clear screen
    Me.DisplayLabel.Text = ""
    'calculate answer
    If mblnAdd = True Then
    Me.DisplayLabel.Text = mintFirstNum + mintSecondNum
    End If
    If mblnSubtract = True Then
    Me.DisplayLabel.Text = mintFirstNum - mintSecondNum
    End If
    If mblnMultiply = True Then
    Me.DisplayLabel.Text = mintFirstNum * mintSecondNum
    End If
    If mblnDivide = True Then
    Me.DisplayLabel.Text = mintFirstNum \ mintSecondNum
    End If


    i changed my equal buttons code to that, but it still wont calculate the answer. Can annyone help me, or does anyonw know a website where i could get some sample code to look at? i think my problem is either with how i'm saving the numbers, or preforming the calculations...please help if you can!!! thanks :)
     
  4. AbvAvgUser

    AbvAvgUser

    Joined:
    Oct 3, 2002
    Messages:
    2,281
    Rather than giving the entire code, just give us the error that you are getting. That will make bug-fixing faster.
     
  5. lamstaldy

    lamstaldy

    Joined:
    Dec 15, 2001
    Messages:
    113
    Try changing the lines that assign values to mintFirstNum and mintSecondNum from:

    Me.DisplayLabel.Text = mintFirstNum
    Me.DisplayLabel.Text = mintSecondNum

    to:

    mintFirstNum = Me.DisplayLabel.Text
    mintSecondNum = Me.DisplayLabel.Text

    mintFirstNum = Me.DisplayLabel.Text takes the number from DisplayLabel and stores it in mintFirstNum.

    Me.DisplayLabel.Text = mintFirstNum takes the value from mintFirstNum and loads it into Me.DisplayLabel.Text.
     
  6. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/169872

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice