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!
man i included your calculator in my program as a button that loads the form calculator
I made reference to you when you press the button that says: calculator
i just added MsgBox ("This calculator was made by Daniel Lara") before loading the form.
If you are in disagree with me adding this function to my program i will remove it, i have no intent of harming you in any way
Its just has all the functionalities i know in one form. Picking a color for background, your calculator, a guessing the number game, a small database, a dice simulator, shuting down your own computer, stopping your computer from shuting down (cool for school lol). Shuting down computers in my school, a secret menu, internet, telnet, notepad and lots of more things, all in a giant form
That's not the calculators fault, it's Visual Basics.
Decimal variables in VB.Net can only hold like 15 to 20 places I think. You can make it go higher by turning Option Strict Off and making Num1, Num2, and Memory strings. For some reason strings hold a ton more digits when used as numbers, they even use automatic scientific notation.
Here is the code completely changed with Strict Off and the variables used as strings. You also need to remove any CDec()s if you are going to do it on your own.
Code:
' Project Name: Calculator
' Programmed By: Daniel Dara on 4/15/09
Option Explicit On
Public Class MainForm
Dim Num1 As String
Dim Num2 As String
Dim Memory As String
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 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 = 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
Case "-"
displayLabel.Text = Num1 - Num2
Case "/"
displayLabel.Text = Num1 / Num2
Case "*"
displayLabel.Text = Num1 * Num2
Case "^"
displayLabel.Text = Num1 ^ Num2
Case "sqrt"
rootButton.PerformClick()
Case "1/x"
reciprocalButton.PerformClick()
Case "+/-"
signButton.PerformClick()
End Select
Num1 = 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 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 = displayLabel.Text
If Num2 <> 0 Then displayLabel.Text = Math.Sqrt(Num2) : Operation = "sqrt"
End Sub
Private Sub reciprocalButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles reciprocalButton.Click
Num2 = displayLabel.Text
If Num2 <> 0 Then displayLabel.Text = (1 / Num2) : Operation = "1/x"
End Sub
Private Sub memorystoreButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles memorystoreButton.Click
Memory = 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 displayLabel.Text <> 0 Then
Memory += 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
If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!
This is only the "Simple" calculator. Though the one on your computer has an "Extended" option, it would too much of a waste of time to code that too since it's already on nearly every computer in the world. This was easy enough and provided enough practice to where I didn't need to add the "Extended" options. And without the Extended stuff, there's nothing else to put on the menu.
If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!