A couple of days ago I whipped up this calculator which I modeled after the Microsoft Standard Calculator. I made it with Visual Basic 2005 so it should be compatible with 2008.
I programmed it exactly like the Microsoft one so it remembers the last operation and when you just press "=" it will execute it again. It even includes the Memory functions. The only two differences are the exponent button instead of the % button and also how decimals are handled. In the Microsoft calculator, the decimal is always shown and you simply allow numbers to be added to the left of it by pressing the decimal button. In my calculator, the decimal is not shown until you press the decimal button (which I think is better).
Here's the code in case you can't open the project files.
Code:
' Project Name: Calculator
' Programmed By: Daniel Dara on 4/15/09
Option Explicit On
Option Strict On
Public Class MainForm
Dim Num1 As Decimal
Dim Num2 As Decimal
Dim Memory As Decimal
Dim Operation As String
Dim isDecimal As Boolean
Dim EraseNext As Boolean
Dim doOperation As Boolean = True
Private Sub any_numberButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles zeroButton.Click, twoButton.Click, threeButton.Click, sixButton.Click, sevenButton.Click, oneButton.Click, nineButton.Click, fourButton.Click, fiveButton.Click, eightButton.Click
Dim btn As Button = TryCast(sender, Button)
If displayLabel.Text = "0" Or EraseNext Then displayLabel.Text = String.Empty : EraseNext = False
displayLabel.Text &= btn.Text
End Sub
Private Sub clearentryButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearentryButton.Click
displayLabel.Text = "0"
isDecimal = False
EraseNext = False
End Sub
Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click
Num1 = 0
Num2 = 0
clearentryButton.PerformClick()
End Sub
Private Sub backspaceButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles backspaceButton.Click
Dim Disp As String = displayLabel.Text
If Disp.Substring(Disp.Length - 1) = "." Then isDecimal = False
If CDec(Disp) <> 0 Then displayLabel.Text = Disp.Remove(Disp.Length - 1)
Disp = displayLabel.Text
If Disp.Length = 0 Then displayLabel.Text = "0"
End Sub
Private Sub operationButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles plusButton.Click, minusButton.Click, divisionButton.Click, multiplicationButton.Click, exponentButton.Click
If Operation <> String.Empty And doOperation Then equalsButton.PerformClick()
Dim btn As Button = TryCast(sender, Button)
Operation = btn.Text
Num1 = CDec(displayLabel.Text)
EraseNext = True
doOperation = True
End Sub
Private Sub equalsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles equalsButton.Click
If doOperation Then Num2 = CDec(displayLabel.Text)
Select Case Operation
Case "+"
displayLabel.Text = (Num1 + Num2).ToString
Case "-"
displayLabel.Text = (Num1 - Num2).ToString
Case "/"
displayLabel.Text = (Num1 / Num2).ToString
Case "*"
displayLabel.Text = (Num1 * Num2).ToString
Case "^"
displayLabel.Text = (Num1 ^ Num2).ToString
Case "sqrt"
rootButton.PerformClick()
Case "1/x"
reciprocalButton.PerformClick()
Case "+/-"
signButton.PerformClick()
End Select
Num1 = CDec(displayLabel.Text)
EraseNext = True
doOperation = False
End Sub
Private Sub signButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles signButton.Click
Dim Disp As String = displayLabel.Text
If CDec(Disp) <> 0 Then Operation = "+/-" : If Disp.StartsWith("-") Then displayLabel.Text = Disp.Remove(0, 1) Else displayLabel.Text = "-" & Disp
End Sub
Private Sub decimalButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles decimalButton.Click
If isDecimal = False Then isDecimal = True : displayLabel.Text &= "."
End Sub
Private Sub rootButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rootButton.Click
Num2 = CDec(displayLabel.Text)
If Num2 <> 0 Then displayLabel.Text = Math.Sqrt(Num2).ToString : Operation = "sqrt"
End Sub
Private Sub reciprocalButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles reciprocalButton.Click
Num2 = CDec(displayLabel.Text)
If Num2 <> 0 Then displayLabel.Text = (1 / Num2).ToString : Operation = "1/x"
End Sub
Private Sub memorystoreButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles memorystoreButton.Click
Memory = CDec(displayLabel.Text)
If Memory <> 0 Then memoryLabel.Text = "M"
End Sub
Private Sub memoryrecallButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles memoryrecallButton.Click
displayLabel.Text = Memory.ToString
EraseNext = True
End Sub
Private Sub memoryclearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles memoryclearButton.Click
memoryLabel.Text = String.Empty
Memory = 0
End Sub
Private Sub memoryaddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles memoryaddButton.Click
If CDec(displayLabel.Text) <> 0 Then
Memory += CDec(displayLabel.Text)
memoryLabel.Text = "M"
Operation = String.Empty
End If
End Sub
Private Sub anyButton_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles zeroButton.MouseUp, twoButton.MouseUp, threeButton.MouseUp, sixButton.MouseUp, signButton.MouseUp, sevenButton.MouseUp, rootButton.MouseUp, reciprocalButton.MouseUp, plusButton.MouseUp, oneButton.MouseUp, nineButton.MouseUp, multiplicationButton.MouseUp, minusButton.MouseUp, memorystoreButton.MouseUp, memoryrecallButton.MouseUp, memoryclearButton.MouseUp, memoryaddButton.MouseUp, fourButton.MouseUp, fiveButton.MouseUp, exponentButton.MouseUp, equalsButton.MouseUp, eightButton.MouseUp, divisionButton.MouseUp, decimalButton.MouseUp, clearentryButton.MouseUp, clearButton.MouseUp, backspaceButton.MouseUp
' Keeps any button from being highlighted, looks nicer.
displayLabel.Focus()
End Sub
End Class
Last edited by Hack; Apr 16th, 2009 at 05:54 AM.
If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!