# Calculator HELP Visual Basic



## creamesoda (Dec 12, 2004)

Okay so I have absolutely no programming experience whatsoever and I need to make a calculator in a few hours here. I've been working on it for weeks and I've asked for help around campus so I've turned to here as a last resort. I've got the basic four functions working for my calculator and I've even got my exponent function working. The problem I'm having is having the textbox be cleared when I press a function key then clicking on a number. How would I write that, what are commands I can write.

If someone can help me...PLEASE!!!

Here is the code I have so far:

Option Explicit On
Public Class Form1
Public FirstNumber As Decimal = 0
Public SecondNumber As Decimal = 0
Public MemoryNumber As Decimal = 0
Public OneNumber As Boolean = True
Public DHit As Boolean = False
Public OT As Operation
Public Enum Operation
Addition = 1
Subtraction = 2
Multiplication = 3
Division = 4
Exponent = 5
Sqrt = 6
End Enum
Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd1.Click
txtDisplay.Text = txtDisplay.Text & "1"
End Sub

Private Sub cmd2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd2.Click
txtDisplay.Text = txtDisplay.Text & "2"
End Sub

Private Sub cmd3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd3.Click
txtDisplay.Text = txtDisplay.Text & "3"
End Sub

Private Sub cmd4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd4.Click
txtDisplay.Text = txtDisplay.Text & "4"
End Sub

Private Sub cmd5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd5.Click
txtDisplay.Text = txtDisplay.Text & "5"
End Sub

Private Sub cmd6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd6.Click
txtDisplay.Text = txtDisplay.Text & "6"
End Sub

Private Sub cmd7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd7.Click
txtDisplay.Text = txtDisplay.Text & "7"
End Sub

Private Sub cmd8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd8.Click
txtDisplay.Text = txtDisplay.Text & "8"
End Sub

Private Sub cmd9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd9.Click
txtDisplay.Text = txtDisplay.Text & "9"
End Sub

Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd0.Click
txtDisplay.Text = txtDisplay.Text & "0"

End Sub

Private Sub cmddecimal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmddecimal.Click
txtDisplay.Text = txtDisplay.Text & "."
If OneNumber Then
txtDisplay.Text = "0."
End If

If FirstNumber <> 0 Then
txtDisplay.Text = "0."
End If

DHit = True
OneNumber = False
End Sub

Private Sub cmdNegative_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNegative.Click

End Sub

Private Sub cmdRoot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRoot.Click
If FirstNumber = 0 Then
FirstNumber = Convert.ToDecimal(txtDisplay.Text)
OneNumber = True
DHit = False
OT = Operation.Exponent
Else
SecondNumber = Convert.ToDecimal(txtDisplay.Text)
CalculateValue()
OT = Operation.Exponent
FirstNumber = txtDisplay.Text
End If

End Sub
Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click

If FirstNumber = 0 Then
FirstNumber = Convert.ToDecimal(txtDisplay.Text)
OT = Operation.Addition
OneNumber = True
DHit = False
Else
SecondNumber = Convert.ToDecimal(txtDisplay.Text)
CalculateValue()
OT = Operation.Addition
FirstNumber = txtDisplay.Text
End If
End Sub

Private Sub cmdSubract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSubract.Click

If FirstNumber = 0 Then
FirstNumber = Convert.ToDecimal(txtDisplay.Text)
OT = Operation.Subtraction
OneNumber = True
DHit = False
Else
SecondNumber = Convert.ToDecimal(txtDisplay.Text)
CalculateValue()
OT = Operation.Subtraction
FirstNumber = txtDisplay.Text
End If
End Sub

Private Sub cmdMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMultiply.Click
If FirstNumber = 0 Then
FirstNumber = Convert.ToDecimal(txtDisplay.Text)
OT = Operation.Multiplication
OneNumber = True
DHit = False
Else
SecondNumber = Convert.ToDecimal(txtDisplay.Text)
CalculateValue()
OT = Operation.Multiplication
FirstNumber = txtDisplay.Text
End If

End Sub

Private Sub cmdDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDivide.Click
If FirstNumber = 0 Then
FirstNumber = Convert.ToDecimal(txtDisplay.Text)
OT = Operation.Division
OneNumber = True
DHit = False
Else
SecondNumber = Convert.ToDecimal(txtDisplay.Text)
CalculateValue()
OT = Operation.Division
FirstNumber = txtDisplay.Text
End If
End Sub

Private Sub cmdEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEqual.Click
If FirstNumber = 0 Then
Return
End If

If SecondNumber = 0 Then
SecondNumber = Convert.ToDecimal(txtDisplay.Text)
End If
CalculateValue()
End Sub
Private Sub CalculateValue()
If SecondNumber = 0 Then
FirstNumber = Convert.ToDecimal(txtDisplay.Text)
End If

If (OT = Operation.Addition) Then
Dim value As Decimal = 0
Try
value = FirstNumber + SecondNumber
Catch ex As Exception
MessageBox.Show("The operation could not be completed because the resultant value is too large.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0, False)
FirstNumber = 0
SecondNumber = 0
Return
End Try
Dim tempstring As String = Convert.ToString(value)
If tempstring.IndexOf(".") = -1 Then
txtDisplay.Text = tempstring + "."
Else
txtDisplay.Text = tempstring
End If
End If

If OT = Operation.Subtraction Then
Dim value As Decimal = 0
Try
value = FirstNumber - SecondNumber
Catch ex As Exception
MessageBox.Show("The operation could not be completed because the resultant value is too large.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0, False)
FirstNumber = 0
SecondNumber = 0
Return
End Try
Dim tempstring As String = Convert.ToString(value)
If tempstring.IndexOf(".") = -1 Then
txtDisplay.Text = tempstring + "."
Else
txtDisplay.Text = tempstring
End If
End If

If (OT = Operation.Multiplication) Then
Dim value As Decimal = 0
Try
value = FirstNumber * SecondNumber
Catch ex As Exception
MessageBox.Show("The operation could not be completed because the resultant value is too large.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0, False)
FirstNumber = 0
SecondNumber = 0
Return
End Try
Dim tempstring As String = Convert.ToString(value)
If tempstring.IndexOf(".") = -1 Then
txtDisplay.Text = tempstring + "."
Else
txtDisplay.Text = tempstring
End If
End If

If (OT = Operation.Division) Then
Dim value As Decimal = 0
Try
value = FirstNumber / SecondNumber
Catch ex As DivideByZeroException
MessageBox.Show("Cannot divide by zero.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0, False)
Return
Catch x As Exception
MessageBox.Show("The operation could not be completed because the resultant value is too large.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0, False)
FirstNumber = 0
SecondNumber = 0
Return
End Try
Dim tempstring As String = Convert.ToString(value)
If tempstring.IndexOf(".") = -1 Then
txtDisplay.Text = tempstring + "."
Else
txtDisplay.Text = tempstring
End If
End If

If (OT = Operation.Exponent) Then
Dim value As Decimal = 0
Try
value = Math.Pow(FirstNumber, SecondNumber)
Catch ex As Exception
MessageBox.Show("The operation could not be completed because the resultant value is too large or an even root was taken of a negative number.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0, False)
FirstNumber = 0
SecondNumber = 0
Return
End Try

Dim tempstring As String = Convert.ToString(value)
If tempstring.IndexOf(".") = -1 Then
txtDisplay.Text = tempstring + "."
Else
txtDisplay.Text = tempstring
End If
End If
If (OT = Operation.sqrt) Then
Dim value As Decimal = 0
Try
value = Math.Pow(FirstNumber, 0.5)
Catch ex As Exception
MessageBox.Show("The operation could not be completed because the resultant value is too large or an even root was taken of a negative number.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0, False)
FirstNumber = 0
SecondNumber = 0
Return
End Try

Dim tempstring As String = Convert.ToString(value)
If tempstring.IndexOf(".") = -1 Then
txtDisplay.Text = tempstring + "."
Else
txtDisplay.Text = tempstring
End If
End If

FirstNumber = 0
SecondNumber = 0
DHit = False
OneNumber = True
End Sub

Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click
txtDisplay.Text = ""
End Sub

Private Sub cmdPercent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPercent.Click

End Sub

Private Sub cmdMemoryAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMemoryAdd.Click
If MemoryNumber = 0 Then
Return
End If

FirstNumber = txtDisplay.Text
SecondNumber = MemoryNumber
OT = Operation.Addition
CalculateValue()
End Sub

Private Sub cmdMemoryRecall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMemoryRecall.Click
If MemoryNumber = 0 Then
Return
End If

Dim curString As String = Convert.ToString(MemoryNumber)
If (curString.IndexOf(".") = -1) Then
curString += "."
End If

txtDisplay.Text = curString

End Sub

Private Sub cmdPower_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPower.Click
If FirstNumber = 0 Then
FirstNumber = Convert.ToDecimal(txtDisplay.Text)
OneNumber = True
DHit = False
OT = Operation.Sqrt
Else
SecondNumber = Convert.ToDecimal(txtDisplay.Text)
CalculateValue()
OT = Operation.Sqrt
FirstNumber = txtDisplay.Text
End If
End Sub

Private Sub MemoryStore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MemoryStore.Click
MemoryNumber = Convert.ToDecimal(txtDisplay.Text)
If MemoryNumber = 0 Then
Return
End If
txtDisplay2.Text = "M"
OneNumber = True
DHit = False

End Sub

Private Sub cmdClearAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClearAll.Click
MemoryNumber = 0
txtDisplay2.Text = ""
txtDisplay.Text = ""
End Sub

Private Sub MemoryClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MemoryClear.Click
MemoryNumber = 0
txtDisplay2.Text = ""
End Sub

Private Sub txtDisplay2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDisplay2.TextChanged
txtDisplay2.Enabled = False
Me.Focus()
txtDisplay2.Enabled = True
End Sub

Private Sub cmdDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDel.Click
Dim index As Integer = txtDisplay.Text.Length

If DHit = True Then
If txtDisplay.Text.IndexOf(".") = index - 1 Then
DHit = False
If txtDisplay.Text = "0." Then
OneNumber = True
End If
Return
Else
txtDisplay.Text = txtDisplay.Text.Substring(0, index - 1)
Return
End If
End If

If index = 2 Then
txtDisplay.Text = "0."
OneNumber = True
DHit = False
Return
End If

Dim num As String = txtDisplay.Text.Substring(0, index - 2)
num += "."
txtDisplay.Text = num
End Sub

End Class


----------



## draceplace (Jun 8, 2001)

Looks like decent code CreamSoda, is what your wanting to do similar to cmdClear or cmdClearAll? Can you call one of those based on the key sequence?


----------

