Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Develop a Currency Converter in the .NET Compact Framework : Page 2

Smart devices require smart applications and in this first of a three-part series on building applications in the .NET Compact Framework, you'll learn how to build one that lets users accurately convert foreign currencies on the fly.




Application Security Testing: An Integral Part of DevOps

Setting Up Tab Controls
For the UI of this application, I want the user to be able to move between two different forms, which are layered behind one another. The TabControl lets me create a file tab metaphor so that only one form is visible and active at any time. My application contains one TabControl control, which in turn contains two TabPage controls. The first TabPage is called "Conversion" while the second is "Set Rates." The TabPage control is maximizes the available screen real estate.

The first "tab" of the application is the default Form1. To add a second TabPage control to a form, select the TabControl control and in the Properties window, click on the Add Tab link, as shown in Figure 3. You can set as many TabPages as you like in your own applications.

Figure 3. Add a New TabPage Control. You'll need to master building TabPages to write sophisticated applications for handheld devices.

Coding the Controls
Now that the forms are set up, we need to begin writing the application logic that will perform the currency translation. Because our sample application deals with three currencies, we need three dataset objects to bind the exchange rate of each currency to the ComboBox controls, where the end users selects the currencies to convert. To do this, switch to code view and declare the following three dataset objects and a global variable.

Dim ds0 As New DataSet Dim ds1 As New DataSet Dim ds2 As New DataSet Dim ready As Boolean = False

Now, I need to tell the application where to get the exchange rate information. I have created an XML file, Rates.xml, which contains the exchange rate for each currency. I'll define a constant for storing this filename.

Const FILENAME = "\My Documents\Personal\Rates.xml"

When the form is first loaded, you need to load the exchange rates by calling the LoadRates() method. After that, bind the dataset objects to the three ComboBox controls.

Private Sub Form1_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load LoadRates(ds0) LoadRates(ds1) LoadRates(ds2) DoBinding(ComboBox1, ds0) DoBinding(ComboBox2, ds1) DoBinding(ComboBox3, ds2) ready = True End Sub

The LoadRates() method tries to load the exchanges rate information from the file, Rates.xml, shown here:

<?xml version=1.0 standalone=yes?> <NewDataSet> <Currency> <Sym>US</Sym> <Rate>1</Rate> </Currency> <Currency> <Sym>RMB</Sym> <Rate>8</Rate> </Currency> <Currency> <Sym>SIN</Sym> <Rate>1.74</Rate> </Currency> </NewDataSet>

The base currency is the U.S. dollar. All other currencies are exchanged relative to the U.S. dollars. For example, $1 U.S. is equivalent to 1.74 Singapore dollars.

If the file does not exist when the application is first loaded, the InitRates() method will load the default exchange rates (for simplicity I am assuming that the values are hard coded; in real life these values should preferably be loaded dynamically, say from a Web service).

Public Sub LoadRates(ByVal ds As DataSet) Try ds.ReadXml(FILENAME) Catch err As Exception InitRates(ds) ' init the rates if rates.xml ' cannot be found End Try End Sub

The InitRates() method creates a dataset containing the Currency table and three default exchange rates. The information is then written to file:

Public Sub InitRates(ByVal ds As DataSet) ds.Tables.Add("Currency") ds.Tables("Currency").Columns.Add("Sym") ds.Tables("Currency").Columns.Add("Rate") Dim row As DataRow row = ds.Tables("Currency").NewRow row("Sym") = "US" row("Rate") = 1 ds.Tables("Currency").Rows.Add(row) row = ds.Tables("Currency").NewRow row("Sym") = "RMB" row("Rate") = 8 ds.Tables("Currency").Rows.Add(row) row = ds.Tables("Currency").NewRow row("Sym") = "SIN" row("Rate") = 1.74 ds.Tables("Currency").Rows.Add(row) ds.WriteXml(FILENAME) End Sub

The DoBinding() method binds the ComboBox control with the dataset.

Public Sub DoBinding(ByVal control As _ System.Windows.Forms.ComboBox, _ ByVal ds As DataSet) control.Items.Clear() control.DataSource = ds.Tables("Currency") control.DisplayMember = "Sym" control.ValueMember = "Rate" End Sub

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date