admin Level: Administrator

 Registered: 04-04-2002 Posts: 600
|
VB.Net Calculator Archived to Disk
This is a simple calculator written in VB.Net Beta 2
Just drop the following code into a new Widows Form class and compile.
If would like the full project source code in a zip file, you can write to me at MotiCohen@yahoo.com
and ask for "VB.Net Calculator.zip" file.
VB.Net is great!
'Created: Nov 4, 2001
'Author : Moti Cohen
'Company: Victoria C, Inc.
'Write comments to:
' MotiCohen@Yahoo.com
Imports System
Imports System.windows.Forms
Namespace nsCalculator
Public Class FormMain
Inherits System.windows.Forms.Form
#Region "Members declarations"
Private btnNumbers(10) As Button 'All the numeric buttons
' All the operator buttons
Private btnDot, btnPlus, btnSubtract, btnMultiply, btnDivide, btnEqual, btnClear As Button
Private panCalculator As Panel 'Panel to hold all the controls
Private txtOut As TextBox 'Output textbox. (Readonly)
Private dblFirstVal As Double 'The first operand, before the operator
Private dblCurrVal As Double 'The new value, as it is being clicked in.
Private boolClearText As Boolean 'Clear the text box for new output
Private isFirstOperand As Boolean 'True when we start calc with first operand
Private strOperator As String
#End Region
#Region "Add controls to the form"
'
' Add and possition all the buttons on the panel
' Called from the constructor
' You can do this work in the form designer, and avoid typing.
'
Private Sub addButtons(ByVal p As Panel)
Dim i As Integer
Dim fntBold As New System.Drawing.Font("Microsoft Sans Serif", 10!,
System.Drawing.FontStyle.Bold, _
System.Drawing.GraphicsUnit.Point, CType(0,
Byte))
For i = 1 To 10 Step 1
btnNumbers(i) = New Button()
btnNumbers(i).Text = i.ToString
btnNumbers(i).Size = New Size(25, 25)
btnNumbers(i).BackColor = Color.White
AddHandler btnNumbers(i).Click, AddressOf Me.btnNumber_Click
p.Controls.Add (btnNumbers(i))
Next i
btnNumbers(10).Text = "0"
btnNumbers(10).Size = New Size(65, 25)
btnNumbers(10).Location = New Point(10, 160)
btnNumbers(1).Location = New Point(10, 120)
btnNumbers(4).Location = New Point(10, 80)
btnNumbers(7).Location = New Point(10, 40)
btnNumbers(2).Location = New Point(50, 120)
btnNumbers(5).Location = New Point(50, 80)
btnNumbers(8).Location = New Point(50, 40)
btnNumbers(3).Location = New Point(90, 120)
btnNumbers(6).Location = New Point(90, 80)
btnNumbers(9).Location = New Point(90, 40)
btnDot = New Button()
btnDot.Size = New Size(25, 25)
btnDot.Location = New Point(90, 160)
btnDot.BackColor = Color.White
btnDot.Text = "."
btnDot.Font = fntBold
AddHandler btnDot.Click, AddressOf Me.btnNumber_Click
btnPlus = New Button()
btnPlus.Size = New Size(25, 25)
btnPlus.Location = New Point(130, 160)
btnPlus.BackColor = Color.White
btnPlus.Text = "+"
AddHandler btnPlus.Click, AddressOf Me.btnOperator_Click
btnSubtract = New Button()
btnSubtract.Size = New Size(25, 25)
btnSubtract.Location = New Point(130, 120)
btnSubtract.BackColor = Color.White
btnSubtract.Text = "-"
btnSubtract.Font = fntBold
AddHandler btnSubtract.Click, AddressOf Me.btnOperator_Click
btnMultiply = New Button()
btnMultiply.Size = New Size(25, 25)
btnMultiply.Location = New Point(130, 80)
btnMultiply.BackColor = Color.White
btnMultiply.Text = "*"
AddHandler btnMultiply.Click, AddressOf Me.btnOperator_Click
btnDivide = New Button()
btnDivide.Size = New Size(25, 25)
btnDivide.Location = New Point(130, 40)
btnDivide.BackColor = Color.White
btnDivide.Text = "/"
AddHandler btnDivide.Click, AddressOf Me.btnOperator_Click
btnEqual = New Button()
btnEqual.Size = New Size(25, 25)
btnEqual.Location = New Point(170, 160)
btnEqual.BackColor = Color.White
btnEqual.Text = "="
AddHandler btnEqual.Click, AddressOf Me.btnEqual_Click
btnClear = New Button()
btnClear.Size = New Size(30, 20)
btnClear.Location = New Point(170, 40)
btnClear.BackColor = Color.Orange
btnClear.Text = "AC"
AddHandler btnClear.Click, AddressOf Me.btnClear_Click
p.Controls.Add (btnDot)
p.Controls.Add (btnPlus)
p.Controls.Add (btnSubtract)
p.Controls.Add (btnMultiply)
p.Controls.Add (btnDivide)
p.Controls.Add (btnEqual)
p.Controls.Add (btnClear)
End Sub
#End Region
#Region "Event handlers"
Private Sub btnNumber_Click(ByVal obj As Object, ByVal ea As EventArgs)
If (boolClearText) Then txtOut.Text = ""
txtOut.Text += CType(obj, Button).Text
If (txtOut.Text = ".") Then txtOut.Text = "0."
Try
dblCurrVal = CDbl(txtOut.Text)
Catch e As Exception
MsgBox (e.Message)
Finally
boolClearText = False
End Try
End Sub
'
Private Sub btnOperator_Click(ByVal obj As Object, ByVal ea As EventArgs)
If (isFirstOperand) Then
strOperator = CType(obj, Button).Text
dblFirstVal = dblCurrVal
Else
calc()
strOperator = CType(obj, Button).Text
End If
isFirstOperand = False
boolClearText = True
End Sub
'
Private Sub btnClear_Click(ByVal obj As Object, ByVal ea As EventArgs)
dblFirstVal = 0
dblCurrVal = 0
isFirstOperand = True
txtOut.Text = ""
txtOut.Focus()
End Sub
'
Private Sub btnEqual_Click(ByVal obj As Object, ByVal ea As EventArgs)
calc()
End Sub
'
Private Sub calc()
Dim dblOut As Double
Select Case strOperator
Case "+"
dblOut = dblFirstVal + dblCurrVal
Case "-"
dblOut = dblFirstVal - dblCurrVal
Case "*"
dblOut = dblFirstVal * dblCurrVal
Case "/"
dblOut = dblFirstVal / dblCurrVal
End Select
strOperator = "="
isFirstOperand = True
txtOut.Text = dblOut.ToString
dblFirstVal = dblOut
dblCurrVal = dblOut
End Sub
#End Region
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
'
'You can do the following work in the form designer, and avoid typing.
'
Try
Me.Text = "Calculator"
Me.MinimizeBox = False
Me.MinimizeBox = False
Me.KeyPreview = True
panCalculator = New Panel()
txtOut = New TextBox()
txtOut.Location = New Point(10, 10)
txtOut.Size = New Size(150, 10)
txtOut.ReadOnly = True
txtOut.RightToLeft = RightToLeft.Yes
panCalculator.Size = New Size(210, 225)
panCalculator.BackColor = Color.Beige
panCalculator.Controls.Add (txtOut)
addButtons (panCalculator)
Me.Size = New Size(210, 225)
Me.Controls.Add (panCalculator)
dblFirstVal = 0
dblCurrVal = 0
isFirstOperand = True
boolClearText = False
strOperator = "-"
Catch e As Exception
MsgBox ("error ... " & ControlChars.CrLf & e.StackTrace)
End Try
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
'Required by the Windows Form Designer
Private components As System.ComponentModel.Container
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Private Sub InitializeComponent()
'
'FormMain
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.name = "FormMain"
End Sub
#End Region
#Region "keyboard implementation"
'
' Work with the keyboard
'
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
Debug.Write(e.KeyChar)
Dim n As Byte
If (IsNumeric(e.KeyChar)) Then
n = CByte(e.KeyChar.ToString)
Select Case n
Case 0 : btnNumbers(10).PerformClick()
Case 1 : btnNumbers(1).PerformClick()
Case 2 : btnNumbers(2).PerformClick()
Case 3 : btnNumbers(3).PerformClick()
Case 4 : btnNumbers(4).PerformClick()
Case 5 : btnNumbers(5).PerformClick()
Case 6 : btnNumbers(6).PerformClick()
Case 7 : btnNumbers(7).PerformClick()
Case 8 : btnNumbers(8).PerformClick()
Case 9 : btnNumbers(9).PerformClick()
End Select
Else
Select Case e.KeyChar
Case "/" : Me.btnDivide.PerformClick()
Case "*" : Me.btnMultiply.PerformClick()
Case "+" : Me.btnPlus.PerformClick()
Case "*" : Me.btnSubtract.PerformClick()
Case "=" : Me.btnEqual.PerformClick()
Case ControlChars.Cr : Me.btnEqual.PerformClick()
Case Convert.ToChar(27) : Me.btnClear.PerformClick() 'Esc
End Select
End If
End Sub
#End Region
End Class
End Namespace
____________________________
AndreaVB
|