WRI Colleges Forum
Hello WRIans...

If you are not a member yet, please register with complete information to be able to post, reply and to create thread or topics in the forum.

Madya na po kita!!

Login with FACEBOOK is now available for facebook users.

Please notify or send an SMS to forum admin @09501527151 for activation of your account.
Welcome...... Alumni of Naga, Rinconada, Calabanga, Partido and Sorsogon....
Alumni of the WRI-Goa Branch is planning to have a simple Alumni Reunion on 2018. Please text 09501527151 for attendance.
We are looking for Forum Moderator from other branches; Naga, Rinconada, Calabanga and Sorsogon Alumni. Salamat.
Display results as :
Advanced Search
October 2019


Latest topics
alumni reunion... Thu Nov 30, 2017 6:33 pmarvie21
Annyeong-haseyoThu Nov 30, 2017 6:28 pmarvie21
hello.... good daySun Nov 19, 2017 6:59 pmChelleAnn
Forum Rules and GuidelinesSun Nov 19, 2017 3:36 pmradax
Uploading of Images and VideosSun Nov 19, 2017 3:34 pmradax
What is PROTOCOL?Sat Jul 29, 2017 6:17 pmradax
Pa-Raffle 2010 @ WRI-Goa GroundMon Jul 17, 2017 8:20 pmradax
Forum Rules & GuidelinesMon Jul 17, 2017 8:17 pmradax
Forum Group SynchronizationMon Jul 17, 2017 8:12 pmradax
The Sportsfest 2014Wed Mar 19, 2014 10:18 amradax
kumusta? goanians???? Thu Mar 13, 2014 10:19 pmGuest
WRI Foundation 2014 at SM City NagaMon Mar 10, 2014 11:47 amGuest
hi everyone..Mon Mar 03, 2014 12:37 pmradax
Care and maintenance of Leather furnitureMon Oct 28, 2013 5:14 pmGuest
Consider the filling of the sofaFri Oct 25, 2013 5:12 pmGuest
Conversion Dining Top for Pool TableTue Oct 22, 2013 4:48 pmGuest
Antique Writing DesksMon Oct 21, 2013 5:01 pmGuest
Take care of your wooden outdoor furniture Sat Oct 19, 2013 4:39 pmGuest
Cooking FeastSat Aug 27, 2011 3:18 pmmherlcan
wri-calabangaFri Jul 08, 2011 6:02 pmrickzmanansala
Have a nice day WriansTue Jul 05, 2011 12:12 pmrickzmanansala
hi po sir danSun Jul 03, 2011 3:48 pmrickzmanansala
asar talaga.. Sun Jul 03, 2011 3:48 pmrickzmanansala
Saang Branch ka ba?Sun Jul 03, 2011 3:34 pmrickzmanansala
WRI Intramurals 2010Thu Nov 04, 2010 4:35 pmmherlcan
What is a Protocol?Tue Oct 26, 2010 12:00 pmBlex
What is a Network?Tue Oct 26, 2010 11:59 amGuest
How to crimp a cable RJ45Tue Oct 26, 2010 11:39 amGuest
Troubleshooting Your Phone LineTue Oct 26, 2010 11:24 amBlex
General printer troubleshooting.Tue Oct 26, 2010 11:08 amGuest
kelan po ba ang intrams ng WRI-Goa?Tue Oct 12, 2010 10:10 amGuest
ehem...Sun Sep 19, 2010 4:35 pmGuest
rationalTue Sep 07, 2010 7:07 amarvie21
Pagsungko ni INA (2010)Sat Sep 04, 2010 1:49 pmGuest
WRI-GOA STARTING POINT...Fri Sep 03, 2010 1:37 amarvie21
WRI-Goa Alumni EventsSun Aug 29, 2010 11:53 amBlex
kumusta wrians?Sun Aug 15, 2010 10:23 pmbonnkeivy
Basic PC AssemblySun Aug 08, 2010 6:59 pmGuest
Propaganda TechniquesSun Aug 08, 2010 6:32 pmGuest
Good news for WRIFri Jan 22, 2010 10:08 amradax
Tictactoe in C++Wed Dec 16, 2009 2:29 amviraxz
WRI Goa momentsThu Dec 10, 2009 3:15 pmGuest
help me my friends.. :DThu Dec 10, 2009 3:01 pmGuest
To all WRIan Graduates...Thu Dec 10, 2009 2:03 pmGuest
Shaker Sort ANSI CTue Dec 08, 2009 4:52 pmjcodee
Alumni Date on the making..Sat Nov 28, 2009 12:02 pmBlex
Simple Calculator in VBFri Nov 27, 2009 6:49 pmGuest
Planning for WRI AlumniThu Nov 26, 2009 11:22 pmGuest
WRI SorsogonFri Nov 20, 2009 2:34 amradax
To all WRIans...Mon Nov 16, 2009 9:12 pmmeowchi
Online Forum MeetingSat Nov 14, 2009 2:31 pmmeowchi
Merry Christmas..Sat Nov 14, 2009 2:28 pmmeowchi
Calling all WRI Graduates!Thu Nov 12, 2009 6:40 pmmeowchi
Updated WRIans Forum SiteFri Nov 06, 2009 4:19 amradax
Pictures of WRIWed Nov 04, 2009 4:08 pmmeowchi
List of names of WRI GraduatesMon Oct 26, 2009 11:11 amradax
Who is online?
In total there is 1 user online :: 0 Registered, 0 Hidden and 1 Guest


Top posters
157 Posts - 43%
42 Posts - 12%
25 Posts - 7%
23 Posts - 6%
23 Posts - 6%
21 Posts - 6%
20 Posts - 6%
20 Posts - 6%
19 Posts - 5%
13 Posts - 4%
Cool Radio Hits

Page Hits
reset 12/3/2017
Simple Calculator in VB Counter

Go down

default Simple Calculator in VB

on Fri Nov 27, 2009 6:49 pm
This tutorial will demonstrate how to code up a simple calculator, note this calculator will manipulate two numbers and will not account for error catching in any great degree.

This tutorial will teach you:-

-Simple Arrays

-Manipulating numbers

-Implementing summing

Let's begin. Firstly we need to create the interface. So you'll need to crank
up ole VB. Put a text box up the top, clear it and give it a decent name like
txtDisplay, now we need to create the numbers, these will be command buttons
and must be an array, the easiest way to do this is to just create one button,
copy and paste it, and when the system asks do you want to create a control
array, you click yes! But what is and array? Well simply put it is a collection
of related variable that share the same type.

Do this for all the numbers 0
through 9, just like on Windows own calculator. Now you need to do the same
for the methods of calculation, the - + * etc. Now you should have something
that resembles a calculator. Of course these aren't the only buttons you'll
need dont forget the equals sign and of course some form of clear, I use C just
like Windows' own calculator. And now onto some coding.

We are going to need three variables, one for the first number of type double
(to hold large numbers.) one for the second number, and of course one for the
sign, or type of calculation we are doing. This is how I have declared them.

Dim first As Double
Dim second As Double
Dim sign As String

Not to bad so far, let's get those nasty arrays out of the way.

Private Sub Command1_Click(Index As Integer)
If txtDisplay.Text = "" Then
= Command1(Index).Caption
= txtDisplay.Text & Command1(Index).Caption
End If
End Sub

This is the array, what this does is makes the content of the display screen
equal to the caption that has been clicked. In other words if you click the
button with 1 on it the number one will go to the display text box. The same
applies for the following array sub.

Private Sub Command2_Click(Index As Integer)
first = txtDisplay.Text
txtDisplay.Text = ""
sign = Command2(Index).Caption
End Sub

Now what this does is assign the value of the display to the first variable,
this makes it nice and easy to manipulate two numbers, it's my tip of the day,
next it clears the content of the display box and then assigns the sign to the
sign variable. And now for a minimal effort at keeping the user from crashing

Private Sub Form_Load()
txtDisplay.Enabled = False
txtDisplay.MaxLength = 10
End Sub

On form load set max length of the text box to 10 and dont let anything get
stuck in the text box by any other method then hitting the command buttons.
Now lets see our hard work pay off by adding the calculation sub.

Private Sub cmdEq_Click()
second = txtDisplay.Text
If sign = "-" Then
= first - second
ElseIf sign = "+" Then
= first + second
ElseIf sign = "*" Then
= first * second
ElseIf sign = "/" Then
= first / second
End If
End Sub

Firstly the sub assigns whats in the display to the variable named second.
It then works out what calculation should be done by finding the current value
for the sign variable, so if the variable is / then it will devide the first
number by the second. And that in essence is the calculator, not to bad I thought,
and easy enough for you to build on and create a much more complex one. There
are a few mor subs we should explore but don't deserve any special attention.

Private Sub cmdDot_Click()
If InStr(txtDisplay.Text, ".") = 0 Then
txtDisplay.Text = txtDisplay.Text & "."
End If
End Sub

This tests if there is a . in the box before adding one.

Private Sub cmdClear_Click()
txtDisplay.Text = "0"
End Sub

Resets the display box.

This is all there is to writing a simple calculator program in VB it's barely
a page worth of actual code.

What you should have learned :-
-Array use
-Simple error cactching
-Using numbers in Visual Basic with mathematical calculations

This article was originally written by Sliptop
Back to top
Permissions in this forum:
You cannot reply to topics in this forum