Results 1 to 10 of 10

Thread: calculator

  1. #1

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,458

    calculator

    here's my vb2008 calculator:

    edit: updated version including upgrade from post #2
    if you find any bugs, please let me know + i'll try to fix them.
    i'm posting known bugs + solutions in post #7
    Attached Images Attached Images  
    Attached Files Attached Files
    Last edited by .paul.; Jul 26th, 2010 at 07:06 AM.

  2. #2

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,458

    Re: calculator

    here's an upgrade, to allow keyboard input.

    set form1.keypreview = true, then add this:

    vb Code:
    1. Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
    2.     Dim ktp = From btn As Button In Me.Controls.OfType(Of Button)() _
    3.               Where btn.Text.ToLower = Char.ToLower(e.KeyChar) _
    4.               Select btn
    5.  
    6.     If ktp.Count > 0 Then ktp.First.PerformClick()
    7. End Sub

  3. #3
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: calculator

    Does it use the expression evaluation code from the CodeBank? You know, the one with the code compilation at runtime...
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  4. #4

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,458

    Re: calculator

    no it doesn't. it's just a simple calculator

  5. #5
    Member
    Join Date
    Jul 2010
    Posts
    35

    Re: calculator

    Does the download contain the source code? My calculator can only work with two textboxes, but I want one.

  6. #6

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,458

    Re: calculator

    Quote Originally Posted by iProRyan View Post
    Does the download contain the source code?
    yes it contains the source code

  7. #7

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,458

    Re: calculator

    heres's a bug i found.
    it allows numbers to be appended to the total + can be fixed by adding a space after the "=" in the endswith:

    Code:
    Private Sub btns_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, btn10.Click, btn11.Click
        If Not Label1.Text.EndsWith("=") Then TextBox1.Text &= DirectCast(sender, Button).Text
    End Sub
    amended code:

    Code:
    Private Sub btns_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, btn10.Click, btn11.Click
        If Not Label1.Text.EndsWith("= ") Then TextBox1.Text &= DirectCast(sender, Button).Text
    End Sub

  8. #8
    Lively Member
    Join Date
    Feb 2010
    Posts
    106

    Re: calculator

    wouldn't this be better for the btns_click event?
    vb Code:
    1. Private Sub btns_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    2.         Handles btn1.Click, btn2.Click, btn3.Click, btn4.Click, btn5.Click, btn6.Click, btn7.Click, btn8.Click, btn9.Click, btn10.Click, btn11.Click
    3.         If Not Label1.Text.EndsWith("= ") Then
    4.             TextBox1.Text &= DirectCast(sender, Button).Text
    5.         Else
    6.             Label1.Text = ""
    7.             TextBox1.Text = ""
    8.             TextBox1.Text &= DirectCast(sender, Button).Text
    9.         End If
    10.     End Sub

  9. #9

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,458

    Re: calculator

    Quote Originally Posted by reconrey View Post
    wouldn't this be better for the btns_click event?
    the way it is at the moment, you can continue your calculations using the total as the first number. i want to keep it that way.

  10. #10

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,458

    Re: calculator

    Quote Originally Posted by .paul. View Post
    the way it is at the moment, you can continue your calculations using the total as the first number. i want to keep it that way.
    ok i thought about it + that would make sense. i made a few other modifications too...

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private lastOperator As String
    4.     Private firstPart As Double
    5.  
    6.     Private previousLastOperator As String
    7.     Private previousLastPart As Double
    8.  
    9.     Private Sub btns_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    10.         Handles btn1.Click, btn2.Click, btn3.Click, btn4.Click, btn5.Click, btn6.Click, btn7.Click, btn8.Click, btn9.Click, btn10.Click, btn11.Click
    11.         If Label1.Text.EndsWith("= ") Then
    12.             btnClear.PerformClick()
    13.         End If
    14.         TextBox1.Text &= DirectCast(sender, Button).Text
    15.     End Sub
    16.  
    17.     Private Sub btnOperator_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    18.         Handles btnOperator1.Click, btnOperator2.Click, btnOperator3.Click, btnOperator4.Click
    19.         Dim nextPart As Double
    20.         Double.TryParse(TextBox1.Text, nextPart)
    21.         If nextPart = 0 Then
    22.             If DirectCast(sender, Button).Text = "-" AndAlso TextBox1.Text = "" Then
    23.                 TextBox1.Text &= "-"
    24.             End If
    25.             Return
    26.         End If
    27.  
    28.         previousLastOperator = lastOperator
    29.         previousLastPart = nextPart
    30.  
    31.         methods.calculate(TextBox1, firstPart, nextPart, lastOperator)
    32.  
    33.         If lastOperator <> "" Then
    34.             Label1.Text &= nextPart.ToString
    35.         Else
    36.             Label1.Text &= firstPart.ToString
    37.         End If
    38.         lastOperator = DirectCast(sender, Button).Text
    39.         Label1.Text &= " " & lastOperator & " "
    40.         TextBox1.Text = ""
    41.     End Sub
    42.  
    43.     Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
    44.         Dim lastPart As Double
    45.         Double.TryParse(TextBox1.Text, lastPart)
    46.         If lastPart <> 0 Then
    47.             methods.calculate(TextBox1.Text, firstPart, lastPart, lastOperator)
    48.             Label1.Text &= " " & lastPart.ToString & " = "
    49.         Else
    50.             methods.changeText(Label1.Text, " = ")
    51.             TextBox1.Text = firstPart.ToString
    52.         End If
    53.         firstPart = 0
    54.         lastOperator = ""
    55.     End Sub
    56.  
    57.     Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
    58.         TextBox1.Text = ""
    59.         firstPart = 0
    60.         lastOperator = ""
    61.         Label1.Text = ""
    62.         previousLastOperator = ""
    63.         previousLastPart = 0
    64.     End Sub
    65.  
    66.     Private Sub btnSquareRoot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSquareRoot.Click
    67.         Dim nextPart As Double
    68.         Double.TryParse(TextBox1.Text, nextPart)
    69.         If nextPart <> 0 Then
    70.  
    71.             methods.calculate(TextBox1, firstPart, nextPart, lastOperator)
    72.  
    73.             If lastOperator <> "" Then
    74.                 Label1.Text &= " " & nextPart.ToString & " √ = "
    75.             Else
    76.                 Label1.Text &= firstPart.ToString & " √ = "
    77.             End If
    78.         End If
    79.         If nextPart = 0 AndAlso firstPart <> 0 Then
    80.             methods.changeText(Label1.Text, " √ = ")
    81.         End If
    82.         TextBox1.Text = Math.Sqrt(firstPart).ToString
    83.         Double.TryParse(TextBox1.Text, firstPart)
    84.         lastOperator = ""
    85.     End Sub
    86.  
    87.     Private Sub btnPercentage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPercentage.Click
    88.         Dim lastPart As Double
    89.         If Double.TryParse(TextBox1.Text, lastPart) AndAlso lastPart <> 0 Then
    90.             methods.calculate(TextBox1.Text, firstPart, lastPart, lastOperator, True)
    91.             If lastOperator <> "" Then
    92.                 Label1.Text &= " " & lastPart.ToString & " % = "
    93.             End If
    94.             Double.TryParse(TextBox1.Text, firstPart)
    95.             lastOperator = ""
    96.         ElseIf lastPart = 0 AndAlso previousLastOperator <> "" Then
    97.             methods.calculate(firstPart, previousLastPart, previousLastOperator)
    98.             lastPart = previousLastPart
    99.             methods.calculate(TextBox1.Text, firstPart, lastPart, previousLastOperator, True)
    100.             methods.changeText(Label1.Text, " % = ")
    101.             Double.TryParse(TextBox1.Text, firstPart)
    102.             lastOperator = ""
    103.             previousLastOperator = ""
    104.             previousLastPart = 0
    105.         End If
    106.     End Sub
    107.  
    108.     Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
    109.         Dim ktp = From btn As Button In Me.Controls.OfType(Of Button)() _
    110.                   Where btn.Text.ToLower = Char.ToLower(e.KeyChar) _
    111.                   Select btn
    112.  
    113.         If ktp.Count > 0 Then ktp.First.PerformClick()
    114.     End Sub
    115.  
    116.     Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    117.         Dim r As New Rectangle(TextBox1.Left - 1, Label1.Top - 1, Label1.Width + 2, Label1.Height + TextBox1.Height + 2)
    118.         ControlPaint.DrawBorder3D(e.Graphics, r, Border3DStyle.SunkenInner)
    119.     End Sub
    120.  
    121. End Class

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