Advertise with us
Control Panel
Forum
Hosting Help
ASP.NET 4.5 & SQL 2012 Hosting
Resources
Notice
Login
Members
Shared Hosting
|
Upgrade
|
Advertise
|
Tutorials
|
Home
»
Resources
»
VB.NET 2005 Calculator
ASP.NET
ADO.NET
Web Services
Remoting
Visual Studio 2005
Error Bank
Interview Questions
Tips & Tricks
XML
HTML
Jscript/Javascript
IIS
Windows
General
Submit Resource or code snippet
... and get
surprise gifts
Win Digital camera, ASP.NET Books, Free softwares!!
VB.NET 2005 Calculator
12 Aug, 2010
Author:
Om Prajapati
Himalaya College of Engineering
Summary
Simple Calculator in VB.NET 2005
.NET Classes used :
'Author Details
'-
'Om Prajapati
'omprajapati@gmail.com
'-
Public Class frmLab2
'Global Variables for frmLab2 class
Private var1 As Double
Private var2 As Double
Private calcOperator As String 'to represent which mathmetical operator is pressed
Private Sub btnNum1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNum1.Click
'Appending pressed number to the contents of txtDisplay TextBox
txtDisplay.Text = txtDisplay.Text + Convert.ToString(1)
End Sub
Private Sub btnNum2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNum2.Click
'This is another method of appending pressed number to the contents of txtDisplay TextBox
txtDisplay.Text += Convert.ToString(2)
End Sub
Private Sub btnNum3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNum3.Click
txtDisplay.Text += Convert.ToString(3)
End Sub
Private Sub btnNum4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNum4.Click
txtDisplay.Text += Convert.ToString(4)
End Sub
Private Sub btnNum5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNum5.Click
txtDisplay.Text += Convert.ToString(5)
End Sub
Private Sub btnNum6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNum6.Click
txtDisplay.Text += Convert.ToString(6)
End Sub
Private Sub btnNum7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNum7.Click
txtDisplay.Text += Convert.ToString(7)
End Sub
Private Sub btnNum8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNum8.Click
txtDisplay.Text += Convert.ToString(8)
End Sub
Private Sub btnNum9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNum9.Click
txtDisplay.Text += Convert.ToString(9)
End Sub
Private Sub btnNum0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNum0.Click
'If the length of txtDisplay TextBox is more than 0 then only permit to insert 0(zero) otherwise it is not useless to insert 0
If txtDisplay.Text.Length > 0 Then
txtDisplay.Text += Convert.ToString(0)
End If
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
'Clearing all the contents of txtDisplay TextBox
txtDisplay.Text =
End Sub
Private Sub btnBackspace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackspace.Click
Dim i As Integer
If txtDisplay.Text.Length > 0 Then
'assigning length of contents of txtDisplay TextBox
i = txtDisplay.Text.Length
'removing 1 String from last position (here it is String because TextBox only display String value)
txtDisplay.Text = txtDisplay.Text.Remove(i - 1, 1)
End If
End Sub
Private Sub btnDot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDot.Click
'we have to check three conditions for inserting .(dot or decimal point)
'if .(dot) is already pressed or not along with other Strings and
'if the content is empty, means we have to insert 0 followed by decimal point that is like (0.)
If txtDisplay.Text.Length > 0 Then
If txtDisplay.Text.Contains(.) Then
txtDisplay.Text = txtDisplay.Text
Else
txtDisplay.Text += .
End If
Else
txtDisplay.Text += 0.
End If
End Sub
Private Sub btnSign_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSign.Click
If txtDisplay.Text.Length > 0 Then
'changing sign of contents of txtDisplay TextBox,
'if previously negative then convert it to positive and vise versa by multiplying with -1
'that is (negative * negative = positive) & (negative * positive = negative)
var1 = -1 * Convert.ToDouble(txtDisplay.Text)
txtDisplay.Text = Convert.ToString(var1)
End If
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
'Firstly if the length of contents of txtDisplay TextBox equals to 0(empty) then operator has nothing to do
If txtDisplay.Text.Length <> 0 Then
If calcOperator = Then
'Initially value of calcOperator is empty because no operator is pressed
'after pressing the Add operator or Add Button, contents of txtDisplay is converted to Double for mathematical operation
var1 = Convert.ToDouble(txtDisplay.Text)
'after pressing operator, txtDisplay TextBox is cleared for inserting next Operand (or String in case of TextBox) for Operation
'clearing the contents of txtDisplay textbox after storing in variable var1
txtDisplay.Text =
Else
'calling Calculator sub procedure or function
Calculate()
End If
'calcOperator is assigned Add as value so that Addition is performed in Calculate() sub procedure
calcOperator = Add
End If
End Sub
Private Sub btnSub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSub.Click
'similar to addition
If txtDisplay.Text.Length <> 0 Then
If calcOperator = Then
var1 = Convert.ToDouble(txtDisplay.Text)
txtDisplay.Text =
Else
Calculate()
End If
calcOperator = Sub
End If
End Sub
Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
'similar to addition
If txtDisplay.Text.Length <> 0 Then
If calcOperator = Then
var1 = Convert.ToDouble(txtDisplay.Text)
txtDisplay.Text =
Else
Calculate()
End If
calcOperator = Multi
End If
End Sub
Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
'similar to addition
If txtDisplay.Text.Length <> 0 Then
If calcOperator = Then
var1 = Convert.ToString(txtDisplay.Text)
txtDisplay.Text =
Else
Calculate()
End If
calcOperator = Div
End If
End Sub
Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
'calling Calculate() Sub Procedure and display the result in txtDisplay textbox
Calculate()
'flushing the previously pressed operator
calcOperator =
End Sub
Public Sub Calculate()
var2 = Convert.ToDouble(txtDisplay.Text)
'depending on the operator pressed, particular operation will be handled
Select Case calcOperator
Case Add
var1 = var1 + var2
Case Sub
var1 = var1 - var2
Case Multi
var1 = var1 * var2
Case Div
var1 = var1 / var2
End Select
'now converting the calculated result to String and display it to txtDisplay TextBox
txtDisplay.Text = Convert.ToString(var1)
End Sub
End Class
Feedbacks about this page from members:
- No Feedbacks yet !! -
Submit Feedback
View All Feedbacks
Total
members
:
250839
Average new registrations per day (in last 7 days):
10
New Registration:
Open
Register Now
Talk to Webmaster
Tony John
Facebook
Google+
Twitter
Advertise
Privacy Policy
Terms of use
Contact Us
SpiderWorks Technologies Pvt Ltd.
All Rights Reserved.