Results 1 to 12 of 12

Thread: [Solved]I need some help with a VB.NET Calculator

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2004
    Location
    United Kingdom
    Posts
    55

    [Solved]I need some help with a VB.NET Calculator

    I have the following code for the decimal button on my calculator...

    dbldisplay = CDbl(lblDisplay.Text)
    lblDisplay.Text = dbldisplay & "."

    This displays a decimal point on the display, but when i click another number, the decimal point disappears.

    Any ideas?
    Last edited by Zimpaz; Nov 20th, 2004 at 08:59 PM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    That's because "71." becomes "71"

    You don't need to convert to double and then assign to text. Since the buttons only add numbers to the textbox, you only need to concatenate the strings.

    For decimal,

    textboxname.Text = textboxname.Text & "."


    Of course, you should be checking if another . exists or not.

    For other numbers, simply

    textboxname.text = textboxname.text & "7"

    Get it?

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2004
    Location
    United Kingdom
    Posts
    55
    thankyou, any idea how i would go about making the + and - keys work? then the equals button

  4. #4
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Is your next project Inventing a Wheel?


    Seriously, if you want to replicate a true calculator you are not going to use the textbox.text as your calculating medium. You use that only to accept input and display results. You will have to use double type (or preferably decimal type) variables working away in the background. You will have to check each keypress and then act accordingly. e.g. if the keypress is 7 you use
    arrNumber(0) =7
    arrResult(0) = 7
    if the next keypress is 3 you use

    arrNumber(0)=arrNumber(0)*10+3
    arrResult(0)=arrResult(0)*10+3

    etc. Obviously you have to keep a record of the numbers to decide which array element to use.

    if the next keypress is + you use
    arrOperand(0)="+"

    and if your next number is 8

    arrNumber(1)=8
    and use select case to put the correct result into
    arrResult(1)

    If the next key press is = you put the value of arrResult(1) into dTemp

    But if the keypress was + - you have to continue with the previous sequence.

    That bit is the easiest part.

    If a keypress is * / or ^ you have to be prepared to stick to the rules of arithmetic and cancel the previous action (unless it was an = ) and come back to it when you have completed the priority operation.


    Get the idea?

    I hope I have said enough to persuade you to forget the idea and go out and buy a calculator. If not we will discuss square roots etc
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2004
    Location
    United Kingdom
    Posts
    55
    It is for a university assignment. We are supposed to be using variables and loops i think.

  6. #6
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Check and find out if you are required to observe the rules of arithmetic. Some very basic calculators do not. They just carry out each instruction sequentially.

    e.g. Following the rules 3+4*5 =23 but sequentially the result is 35.

    If you don't have to follow the rules then the project is much simplified, but please don't expect me to use it as a calculator

    Have you followed what I posted?
    Last edited by taxes; Nov 20th, 2004 at 09:00 PM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2004
    Location
    United Kingdom
    Posts
    55
    Thanks for your help guys, i have managed to bodge together some code that works. Expect me back next time i have an assignment :0)

    Zimpaz

  8. #8
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    It would be interesting to see your code. I thought it would take much longer.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  9. #9

    Thread Starter
    Member
    Join Date
    Oct 2004
    Location
    United Kingdom
    Posts
    55
    Its only a basic calculator as it can only add, take away, divide or multiplay 2 numbers. Here is the code for your enjoyment

    Dim number1, number2, operator As String

    Private Sub btnZero_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnZero.Click
    lblDisplay.Text = lblDisplay.Text & "0"
    End Sub

    Private Sub btnOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOne.Click
    lblDisplay.Text = lblDisplay.Text & "1"
    End Sub

    Private Sub btnTwo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTwo.Click
    lblDisplay.Text = lblDisplay.Text & "2"
    End Sub

    Private Sub btnThree_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThree.Click
    lblDisplay.Text = lblDisplay.Text & "3"
    End Sub

    Private Sub btnFour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFour.Click
    lblDisplay.Text = lblDisplay.Text & "4"
    End Sub

    Private Sub btnFive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFive.Click
    lblDisplay.Text = lblDisplay.Text & "5"
    End Sub

    Private Sub btnSix_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSix.Click
    lblDisplay.Text = lblDisplay.Text & "6"
    End Sub

    Private Sub btnSeven_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSeven.Click
    lblDisplay.Text = lblDisplay.Text & "7"
    End Sub

    Private Sub btnEight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEight.Click
    lblDisplay.Text = lblDisplay.Text & "8"
    End Sub

    Private Sub btnNine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNine.Click
    lblDisplay.Text = lblDisplay.Text & "9"
    End Sub

    Private Sub btnDecimal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecimal.Click
    lblDisplay.Text = lblDisplay.Text & "."
    End Sub

    Private Sub btnPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlus.Click
    number1 = lblDisplay.Text
    operator = "+"
    lblDisplay.Text = ""
    End Sub

    Private Sub btnMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMinus.Click
    number1 = lblDisplay.Text
    operator = "-"
    lblDisplay.Text = ""
    End Sub

    Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
    number1 = lblDisplay.Text
    operator = "*"
    lblDisplay.Text = ""
    End Sub

    Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
    number1 = lblDisplay.Text
    operator = "/"
    lblDisplay.Text = ""
    End Sub

    Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
    number2 = lblDisplay.Text
    If operator = "+" Then
    lblDisplay.Text = (Val(number1) + Val(number2))
    ElseIf operator = "*" Then
    lblDisplay.Text = (Val(number1) * Val(number2))
    ElseIf operator = "-" Then
    lblDisplay.Text = (Val(number1) - Val(number2))
    ElseIf operator = "/" Then
    lblDisplay.Text = (Val(number1) / Val(number2))
    End If
    End Sub

    Private Sub btnC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnC.Click
    lblDisplay.Text() = ""
    number1 = ""
    number2 = ""
    operator = ""
    End Sub

    Private Sub btnCE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCE.Click
    lblDisplay.Text() = ""
    End Sub

    Private Sub btnSign_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSign.Click
    Dim intpos As Integer
    intpos = InStr(lblDisplay.Text, "-")
    If intpos = 0 Then
    lblDisplay.Text() = "-" & lblDisplay.Text
    Else
    lblDisplay.Text() = Val(lblDisplay.Text) * -1
    End If

    End Sub
    End Class

  10. #10
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Fine. You could reduce the code by having one sub with several handles to respond to the numerical button click events. Something like

    Private Sub btnNumbers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnZero.Click, btnOne.Click .........etc
    lblDisplay.Text = lblDisplay.Text & CStr(CType(sender, Button).Text)
    End Sub
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  11. #11
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Also, your C & CE buttons need to clear the appropriate variables as well as the textbox.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  12. #12

    Thread Starter
    Member
    Join Date
    Oct 2004
    Location
    United Kingdom
    Posts
    55
    they do, C clears the display, and the 3 variables. CE only clears the display because it means clear entry

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width