Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
Resources » Code Snippets » Application windows, menus & toolbars »
Calculator - vb.net
|
This code shows how to develop a calculator in VB.NET
Option Strict On Option Explicit On
Imports System.ComponentModel Imports System.Drawing Imports System.Windows.Forms Imports Microsoft.VisualBasic Imports System.Globalization
Public Class Calculator Inherits System.Windows.Forms.Form
'Data variables. Private mOp1, mOp2 As Double 'Previously input operand. Private mNumOps As Integer 'Number of operands. Private mLastInput As Operation 'Indicate type of last keypress event. Private mOpFlag As String 'Indicate pending operation. Private mOpPrev As String 'Previous operation Private mMinus As String 'Minus operator "-" Private mDecValue As String 'Local systems decimal value Private mAllowBackSpace As Boolean 'Allow backspace
Private Enum Operation None = 0 Operand [Operator] CE Cancel End Enum
Public Sub New() MyBase.New()
Calculator = Me
'This call is required by the Win Form Designer. InitializeComponent()
'TODO: Add any initialization after the InitializeComponent() call. Reset()
'Get decimal separator based upon machine local settings. mDecValue = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator CalcDec.Text = mDecValue
mMinus = "-"
End Sub
'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub
'BS click event. Private Sub CalcBS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcBS.Click Dim Value As String
'Check if we can backspace before removing items from CalcField.text. If mAllowBackSpace Then Value = CalcField.Text
If Len(Value) > 1 Then Value = Mid(Value, 1, Len(Value) - 1) Else Value = "0" End If
' If all we are left is the minus sign, reset. If Value = "-" Then Value = "0" End If
CalcField.Text = Value End If End Sub
'Cancel click event. Private Sub CalcCan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcCan.Click Reset() End Sub
'Cancel entry click event. Private Sub CalcCE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcCE.Click If (mLastInput.Equals(Operation.Operand)) Then CalcField.Text = "0" ElseIf (mLastInput.Equals(Operation.[Operator])) Then mOpFlag = mOpPrev End If
mLastInput = Operation.CE End Sub
'Handles +, -, *, /, = Private Sub CalcRes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcRes.Click
'Now do not allow backspace(CalcBS) actions after the result has been 'calculated and displayed. mAllowBackSpace = False
If (mLastInput.Equals(Operation.Operand)) Then mNumOps = mNumOps + 1 End If
Select Case mNumOps Case 1 mOp1 = Double.Parse(CalcField.Text)
Case 2 mOp2 = Double.Parse(CalcField.Text)
Select Case mOpFlag Case "+" mOp1 = mOp1 + mOp2
Case "-" mOp1 = mOp1 - mOp2
Case "*" mOp1 = mOp1 * mOp2
Case "/" If (mOp2 = 0) Then MsgBox("Can't divide by zero!", MsgBoxStyle.OKOnly, "Calculator") Else mOp1 = mOp1 / mOp2 End If
Case "=" mOp1 = mOp2 End Select
CalcField.Text = mOp1.ToString() mNumOps = 1 End Select
mLastInput = Operation.[Operator] mOpPrev = mOpFlag
Dim ButtonPressed As Button ButtonPressed = CType(sender, Button)
mOpFlag = ButtonPressed.Text End Sub
Private Sub Operators_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcPlus.Click, CalcSub.Click, CalcMul.Click, CalcDiv.Click CalcRes_Click(sender, e) End Sub
'+/- click event. Private Sub CalcSign_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcSign.Click If (mLastInput <> Operation.Operand) Then CalcField.Text = "0" Else With CalcField If (Mid(.Text, 1, 1)) = mMinus Then .Text = Mid(.Text, 2) Else If .Text <> "0" Then .Text = mMinus & .Text End If End If End With End If
mLastInput = Operation.Operand End Sub
Private Sub AllDigits_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calc0.Click, Calc1.Click, Calc2.Click, Calc3.Click, Calc4.Click, Calc5.Click, Calc6.Click, Calc7.Click, Calc8.Click, Calc9.Click CalcNum_Click(sender, e) End Sub
'0 - 9, ., click event. Private Sub CalcNum_Click(ByVal sender As Object, ByVal e As System.EventArgs)
mAllowBackSpace = True 'now allow backspace
If (mLastInput <> Operation.Operand) Then CalcField.Text = "0" End If
Dim SelButton As Button SelButton = CType(sender, Button) FormatEditField(SelButton.Text)
mLastInput = Operation.Operand End Sub
'Exit menu handler. Private Sub ExitMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitMenu.Click Application.Exit() End Sub
'Private helpers. 'Helper to format the entry in the text box! Private Sub FormatEditField(ByVal NewChar As String) Dim Value As String Value = CalcField.Text
'Determine if there are more than one .'s If (NewChar = mDecValue) Then 'If it's found. If (InStr(Value, mDecValue) > 0) Then 'Don't do anything. Return End If Value = Value + mDecValue Else If Value = "0" Then Value = NewChar Else Value = Value + NewChar End If
End If
CalcField.Text = Value End Sub
' Helper to initialize the vals. Private Sub Reset() mOp1 = 0 mOp2 = 0
mNumOps = 0 mLastInput = Operation.None
mOpFlag = "" mOpPrev = ""
CalcField.Text = "0"
mAllowBackSpace = True End Sub
End Class
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|