0% found this document useful (0 votes)
199 views16 pages

Currency Converter

This document provides instructions for creating a currency converter application in Visual Basic. It discusses adding components like text boxes and labels to the interface to enter currency rates and amounts. It then explains how to code the button click event to convert the amount by getting the input values from the text boxes, multiplying them together, and displaying the result on a label. The tutorial walks through building the interface, adding event handling code, and modifying the output to allow converting between different currencies by selecting the "from" and "to" values in text boxes.

Uploaded by

Rohan Bhadvankar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
199 views16 pages

Currency Converter

This document provides instructions for creating a currency converter application in Visual Basic. It discusses adding components like text boxes and labels to the interface to enter currency rates and amounts. It then explains how to code the button click event to convert the amount by getting the input values from the text boxes, multiplying them together, and displaying the result on a label. The tutorial walks through building the interface, adding event handling code, and modifying the output to allow converting between different currencies by selecting the "from" and "to" values in text boxes.

Uploaded by

Rohan Bhadvankar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Visual Basic Currency converter

Welcome to the currency converter tutorial in visual basic. In this tutorial we will be using Visual studio 2010
(you can get for free or even the 2015 version) with visual basic programming language. This will be the second
currency converter we create in visual basic. The last one we made used Radio buttons to change the currency
rates. In this one we will be making one which you can dictate the currency rates and then make sure it does the
maths accurately.

Let's get started.


First create a new visual basic windows form application in visual studio and call it currency converter.

OVsu Sucío xhern

For this project we will need the following

Component Description Name


Textbox To enter currency rate TextBox1
Textbox To enter country of the TextBox2
currency
Textbox To enter the amount TextBox3
Button Calculate the maths Button1
Label Show country Label1
Label Currency Rate Label2
Label Amount Label3
Label Show result Label4
(Note this is not the final list -we will add more components later on)

Here is the flow chart to explain the program

More tutorial on www.mooict.com


start

Start the app

Enter Country

No

Enter
Conversion

Click
Enter Amount
calculate

Yes

Times conversion rate with


end
amount and return result

Algorithm

Start the App


Enter country
Enter the conversion rate
Enter the amount to convert

User Clicked button


Times conversion rate with amount and return a total
END
User didn't click button
Return back to the main window
ENI

More tutorial on www.mooict.com


Interface

Drag and dop all the components from the toolbox

Pointer RichTetBox
vdLEIIIErLKet

Button TetBox
A Label
A LinkLabel CheckBox ToolTip

Fom

Label1

Label2

Label3

Autonl

Label4

HAs you can we have our 4 labels, 3 text boxes and 1 button for the
interface of this application.

Lets start changing the texts of the labels on screen. Click on label1 and look into the properties window. You
will find an option called text. Inside that we can add any text which will change Label1.
Properties
Labell System.Windows.Foms.La

Location 68,43
Locked False
Forml
Margin 3,0, 3, 0
MaximumSize 0, 0
DMinimumSize 0, 0
Modifiers Friend TCourtry
Padding 0,0,0,0
RightToleft No
Labe2
D Size 39, 13
Tablndex 4
Labe3
Tag
Text Country
TetAlign TopLeft
UseCompatib False
UseMnemoni True Buton1
UseWaitCursc False
Visible True
Label4
Text
The tet associated with the control.

More tutorial on www.mooict.com


In the properties window there are more options where you can change the font and the colour of the text. We
will get to that later on.

Forml1

Country

Conversion Rate

Amount

Button 1

Total:

Here is the final view of what the program should look like now with the text changed. You can change the
button text as well. Let's give it a text of calculate in it since it will calculate the conversion rate for us.

Calculate

Now run the GUl to see if everything is in order. There are few ways to run an app in visual studio.
1) You can run it by access the debug option in the main menu.

o currencyConverter - Microsoft Visual Studio


File Edit View Project Suild Debug Team Data
Windows

Start Debugging F5
Start Performance Analysis
Attach Unity Debugger
2) Click on the green play button on the tool bar
o0 curencyConverter -Microsot Visual Studio
File Edit View Project Build Debug Team Data Format Tools Test Analyze Window Help
10 Deb
3) Lastly just press F5

See if everything is to your liking and then follow along.


Now then with the interface out of the way we can concentrate on the coding.

More tutorial on www.mooict.com


Out program will interact with the user through the button. So what we need to do is double click on the button
add an event to it. Inside that event we can add all of our programming logic.

d Calaulate b

Double click on this

You will see this Screen. This is where all the programming codes go in. Our main priority of to convert the
currency user put in the boxes and return the converted value to them.

Public Class Form1

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

Dim conversionRate As Double = Convert.ToDouble(TextBox2.Text)

Dim totalAmount As Double = Convert.ToDouble(TextBox3.Text)

End Sub
End Class
Add the two highlighted lines in the button1_click function. We have created two double variables to store the
information from the text boxes. Text box 2 will hold the conversion rates and text box 3 holds the amount to be
converted. The reason we have created them as double is because normal integers cannot hold decimal
numbers.

Integers only hold 1, 2, 400, 321, 834298 etc. it cannot hold values such as 22.8 or 1.99 etc. this is why we need
to have a double variable.

Public Class Form1

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

More tutorial on www.mooict.com


Dim conversionRate As Double = Convert.ToDouble(TextBox2.Text)

Dim totalAmount As Double = Convert.ToDouble(TextBox3.Text)

Dim convertedTotal As Double = conversionRate* totalAmount

End Sub
End Class
Lets create another double variable this one will hold the converted total of the amount. As you can see inside
the double variable we created called convertedTotal we are calculating it by conversionRate times by
totalAmount.

Lets say we want to convert dollars to pounds

For this purpose 1 pound is equals to 2.50 dollars. So if we had 11 pounds to convert to dollars how much will
we have?

11 (times) 2.50 = 27.50 Dollars


Makes sense? Yes

Public Class Form1

Private Sub Button1 Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

Dim conversionRate As Double = Convert.ToDouble(TextBox2.Text)

Dim totalAmount As Double = Convert.ToDouble(TextBox3.Text)

Dim convertedTotal As Double = conversionRate * totalAmount

Label4.Text = "From" & TextBox1.Text &" at" &conversionRate &" to Pounds in the amount of " &
totalAmount &", Total is: "& convertedTotal
End Sub
End Class

Now here is the results line. In this line we are taking label4 and changing the text dynamically to suit our
purpose. As you can see first we are calling the label4.text which handles all the string inside the label. We will
change it to show for example "Converted from US at 2.50 to pounds in the amounts of 11 total is 27.50".

More tutorial on www.mooict.com


Curreney Converter

Courty

Canverson Rate 259

Amourt 11

Caicate

Fiom US at 25 to Pounda in the amourt d 11, Ta li 275

Result is accurate.

This is a working currency converter now. However this program will only convert every other currency to
POUND so we need a system where we can change between pound to others as well.

In order to that we will need to another textbox and then change the GUl to the following.
Curency Cervene Changed country to from

textBox4

Cacue

Taal

We have added another text box and label to the program and changed the value of country to "From" and the
other label to "To". Now we can manipulate the program to show which currency and from where it was
converted.

When you are making changes make sure you understand the changes I made to the GUI. Lets make some
changes to the code to reflect our new components.

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click


Dim conversionRate As Double = Convert.ToDouble(TextBox2.Text)

Dim totalAmount As Double = Convert.ToDouble(TextBox3.Text)

Dim convertedTotal As Double = conversionRate * totalAmount

Label4.Text ="From" &TextBox1.Text &" at "& conversionRate &" to "&TextBox4.Text &" in the amount of "
& totalAmount &", Total is: "& convertedTotal

More tutorial on www.mooict.com


End Sub

Look at the highlighted code in the box. We are calling the value of text box 4 which is the TO information box in
the GUI. Now lets run the same calculation as before from US to UK rate is at 2.50 amount of 11

Currency Comvertes SMART 0nk # o

From US

Te UK

Corversion Rate 250

Amourt 11

Calculate

From US at 25to UKin the amourt of 11, Total is 27.5

Now the program shows both currencies.


Lets convert from UK to US dollars now.

Accordin to GOOGLE the exchange rate is


1 Elitish Pound equnls

1.46 US Dollar

1 us Dellar

0 60 Bian Pound
2012 2013 2014 20Is 2016

1 dollar is equals to 0.68 pounds. So let's do the conversion of 11 dollars to pound.

74

More tutorial on www.mooict.com


1.46 US Dollar

Our program seems to be off by a few pence however you need to rememnber that the money exchange rates
are calculating with several decimal points now just 2. So overall the program works and it outputs the right
amount.

Lets look another flow chart with the new GUl settings.

start

Start the app

Enter From

Currency

Enter To
Currency No

Enter
Conversion

Click
Enter Amount
calculate

Yes

Times conversion rate with


end
amount and return result

Its stillthesame as it was before only added the Enter from currency and Enter to currency. The overall program
will work exactly as before.

More tutorial on www.mooict.com


Now the program needs a help screen

HELPER CAT IS HELPING

Every program needs a help screen. Now time to add another button called help to the GUI.

Cunency Comeete

Fign

To

Canverson Fate

Anourt

Caice

Total:

Also we need to add another FORM to the project.

More tutorial on www.mooict.com


Right click on the project

Add > Windows Form

More tutorial on www.mooict.com


OName it help and click on add

Solution Explorer

currencyConverter
My Project
EForm1.vb
help.vb

IInside your solution explorer there is help.vb file. That is another form just
like the one we have been working on before. We will now right click on the help.vb

Seltien Espiores

currencyaverter
a MyPraet

Open
Open Wth..
Via Cede
Vie Designer Sh
Vew Cless Diagar
Eckude From Projecd
Cut Ctrte)

a Coey
Delete

Renarne
Piopetes At.Fnter
lclick on view designer.

More tutorial on www.mooict.com


help

This is the new empty form. It's time to add some helpful
tips here for the user right.

Let's add a label and call it help. And then add a text box.

help
Helo
D

See that little triangle on the corner of the text box. Click on that and select Multiline.

Help
TextBox Tasks
Multiline

Instead of writing a line in different labels we can do it this way. Much simpler.

More tutorial on www.mooict.com


help
Helo
Do

Now stretch it to fit the box.

Here is the help screen text.

In this application you can change currencies between countries.


Enter the country of the currency first
Enter the country you want to convert it to
Enter the conversion rate such as the day exchange rate of the currencies.
Enter the total amount to convert
click on calculate.
Click on the textbox and check the properties window for Text option.

Properties
TertBoxl System.Windows.Forme

Modifie Friend
Multiline True
PasswordCha
ReadOnly False
RightToleft No
ScrolBars None
ShortcutsEnal True
> Sze 256, 210
Tablndex 1

TabStop True
Tag
Tet ncakkuate.
In this application you can change currencies between countries.
Enter the country of the currency first
tt
Ente
e t e ch as the eday eschange rate of the currencies.
Enter the total amount to convert
Cick on calculate

Thetet assoCHted wth the controL.

13:03
24/05/016

Now we need to change one more option in this which is ENABLED.

More tutorial on www.mooict.com


Properties
TextBox1 System.Windows.Form
21 |
CausesValidat True
CharacterCas Normal
ContetMenu (none)
Cursor Beam
Dock None
Enabled True
D Font True
ForeColor False
GeneratelMen
True
HideSelectior True
IChange the enabled from true to false because it disables the textboOx and does
not allow other users to edit the text.

Now time to link both forms together.

Help
Double click on the help button.

Private Sub Button2 Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

End Sub
This code above will be added automatically. Now add the following code in the middle of the program.

Private Sub Button2 Click(sender As System.Object, e As System. EventArgs) Handles


Button2.Click

Dim helpScreen As help = New help()


helpscreen.Show()
End Sub

Here we are creating a variable called helpScreen and then we are giving a data type of help. Remember when
we called that form help right it's the same one. We are stating that this helpScreen should be a new instance of
the help form which we created earlier. This way the program will make space in the memory for the new form
to be deployed to the screen.

Secondly we are calling our newly created helpScreen variable and accessing the built in function called show.
Lets test the prOgram noW.

More tutorial on www.mooict.com


Cuecy Coere SMART ink

heip SMAFT hkled

eete0yty te ouwo
Canuern Rate
earrgt
Anourt

Cloierl

Okay then its all done and now gO make any changes you want to.

If you have managed to get this far then

Hai 5?

Don't leave me hanging yO.....

You might also like