Skip to main content

Past Blast

Featured Products

Windows Mobile Developer Controls
Windows Mobile Developer Controls
Stay in touch using the DEVBUSS RSS feeds.
 

News

Windows Mobile Developer Controls
Sapphire Soltuions

Use Microsoft eVB (eMbedded Visual Basic) to implement a Currency Converter - Part I.

Written by David Carnsew  [author's bio]  [read 45109 times]
Edited by Derek

Page 1  Page 2  Page 3 

The next lines of code we set up an array and assign it to a variable "myString".

For i = 0 To 249 'Run this routine 250 times
myString = myArray(i)

Next, we check our variable "myString" to see if any of the text is starts with an Alpha character. This is done by checking our variable "myString" is equal or greater than (=>) an "A" [ Chr(65) ] AND equal or less than (<=) a "Z" [ Chr(90) ]. If "myString" contains an Alpha character, it is added (cboFrom.AddItem) to the combo boxes (cboFrom, cboTo). The idea behind this is to add all the countries to be displayed in the combo boxes.

e.g. Australia Dollar, US Dollar, Austrian Shilling etc.

If "myString" is equal or greater than "0" [ Chr(48) ] but less or equal to a "9" [ chr(57) ] then this number is added (cboCurrency.AddItem) to our cboCurrency combo box. This combo box is used to perform our currency conversions, and is invisible at run-time.

e.g. 1.0 , 0.5223, 0.06211 etc.

If myString >= Chr(65) And myString <= Chr(90) Then
cboFrom.AddItem myString
cboTo.AddItem myString
ElseIf myString >= Chr(48) And myString <= Chr(57) Then
cboCurrency.AddItem myString
End If
Next

The last part of our code for the Form_Load Event is as follows:

File1.Close

This simply closes our open file (Currency.txt)

lblFileDate.Caption = cboFrom.List(0)

The above line simply displays the date on the screen of when the last "Currency.txt" file was updated. This enables the user to see how current the exchange rates are at the time on entry.

"Current as of 30/11/2000"

The next two lines simply delete the first entry in the text file out of the combo boxes.

cboFrom.RemoveItem (0)
cboTo.RemoveItem (0)

Lastly, the next four lines display the default country in our combo boxes at start up.

cboFrom = "US Dollar"
cboTo = "Australian Dollar"
cboFrom.ListIndex = 0
CboTo.ListIndex = 1

Creating a re-usable Function

Private Function CalculateRate()
Dim a, b, c, d
If TxtFrom.Text = "" Then TxtFrom.Text = 1
a = cboFrom.ListIndex
b = cboTo.ListIndex
c = cboCurrency.List(b)
d = cboCurrency.List(a)

lblTo.Caption = CCur(txtFrom.Text * c / d)

End Function

Instead of typing the same code two or three times throughout our program, we will create a "Function" with this code in it, and call it several times throughout our program.
The second and third lines get the INDEX value and assign it to our variable/s (a,b). This will be used as a look-up type table.

a = cboFrom.ListIndex
b = cboTo.ListIndex

The third and fourth lines assign the text values that are found using these listindex values, to variables c and d.

c = cboCurrency.List(b)
d = cboCurrency.List(a)

The last line of this function is where everything all ties together to perform the currency conversion calculation.

lblTo.Caption = CCur(txtFrom.Text * c / d)

The "CCur" statement converts our number value into a currency value, it then take the value inputted by the user in the "txtFrom" text box and performs the calculation based on the numbers assigned to our variables "c" and "d".

Performing the calculation on-the-fly

Private Sub txtFrom_Change()
If txtFrom.Text = "" Then
lblTo.Caption = ""
Else
CalculateRate 'Run the function if a value is inputted
End If
End Sub

In order for the calculation to take place immediately upon the user inputting a number, we use the "Change" event on our "txtFrom" box.

We use an If..then..Else routine to determine if the user has typed in a number into the "txtFrom" text box. As soon as the "txtFrom" has changed its current value, it will run the function we created earlier (CalculateRate). If no change (or no user input) then our function does not get run, so nothing happens (this would occur at start up).

Private Sub cboFrom_Click()
CalculateRate
End Sub

Private Sub cboTo_Click()
CalculateRate
End Sub


The above two events are assigned to each combo box (cboFrom & cboTo). I have made use of our "CalculateRate" function to perform the currency conversion as soon the user changes the country selection in our combo boxes.

Conclusion

Well, l hope you enjoyed this tutorial on creating a currency converter in eVB as much l did in writing it. Experiment around with the form layout, l have created something simple pictured below which l believe is pleasing to the eye.

Previous Page