Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

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

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.


advertisement
Accepting Data Entry
Users enter the amount of currency they wish to convert using the buttons 0 to 9 on the first TabPage, which invokes the cmdButtons event. Note that this event is fired whenever one of the 10 buttons is pushed. It must also perform error checking so that users may not enter illegal entries, such as multiple decimal points [.]. Once the button is clicked, the value appends the TextBox control, to show the user what numbers he has entered:

Private Sub cmdButtons _ (ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles cmd0.Click, cmd1.Click, cmd2.Click, _ cmd3.Click, cmd4.Click, cmd5.Click, _ cmd6.Click, cmd7.Click, cmd8.Click, _ cmd9.Click, cmdPt.Click If txtValue.Text.Length > 7 Or _ (txtValue.Text.IndexOf("0") = 0 And _ CType(sender, Button).Text = "0") Or _ (txtValue.Text.IndexOf(".") >= 0 And _ CType(sender, Button).Text = ".") Then ' remove ' multiple "." Else If txtValue.Text.IndexOf("0") = 0 Then txtValue.Text = "" End If txtValue.Text += CType(sender, Button).Text End If End Sub

When the value in the TextBox control is changed, indicating that the user has entered another number, the conversion amount must be recalculated. The UpdateValue event handles recalculation when any of these events occur:
  • The selection in ComboBox1 is changed.
  • The selection in ComboBox2 is changed.
  • The value in the TextBox control is changed.



Private Sub UpdateValue _ (ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles ComboBox1.SelectedIndexChanged, _ txtValue.TextChanged, _ ComboBox2.SelectedIndexChanged If ready And txtValue.Text <> "" Then Try lblResult.Text = CSng(txtValue.Text) * _ CSng(ComboBox2.SelectedValue) / _ CSng(ComboBox1.SelectedValue) Catch ex As Exception 'do nothing End Try End If End Sub

The cmdBackSpace_Click() method is fired when the Back Space button is clicked. It is used to remove the last digit that was entered:

Private Sub cmdBackSpace_Click _ (ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles cmdBackSpace.Click ' length must be greater than 1 for deleting If txtValue.Text.Length > 0 Then txtValue.Text = Mid(txtValue.Text, 1, _ txtValue.Text.Length - 1) End If ' set to 0 if length is zero If txtValue.Text.Length = 0 Then txtValue.Text = 0 End If End Sub

The Clear button resets the TextBox control to the value "0":

Private Sub cmdClear_Click _ (ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles cmdClear.Click txtValue.Text = 0 End Sub

The "=" button adds the current conversion into the ListBox control.

Private Sub cmdEq_Click _ (ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles cmdEq.Click Dim str As String str = txtValue.Text & ComboBox1.Text & " = " _ & lblResult.Text & ComboBox2.Text ListBox1.Items.Add(str) End Sub

The Clear History menu item simply clears the items in the ListBox control:

Private Sub MenuItem2_Click _ (ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MenuItem2.Click ListBox1.Items.Clear() End Sub

The second TabPage control for the sample application allows the user to change the exchange rate of each currency. In production, this would frequently be done via a Web service, which would grab the latest rates. In this case users can override the rates included in the rates.xml file. When ComboBox3 control is selected (see Figure 1), the rate for the respective currency is displayed.

Private Sub ComboBox3_SelectedIndexChanged _ (ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles ComboBox3.SelectedIndexChanged txtRate.Text = ds2.Tables("Currency").Rows _ (ComboBox3.SelectedIndex).Item _ ("Rate").ToString End Sub

The Update button will cause all the Dataset objects to be updated and reflect the newly entered rate. It will then call the saveRate() method to save the changes to the Rates.xml file.

Private Sub cmdUpdate_Click _ (ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles cmdUpdate.Click ds0.Tables("Currency").Rows _ (ComboBox3.SelectedIndex).Item _ ("Rate") = txtRate.Text ds1.Tables("Currency").Rows _ (ComboBox3.SelectedIndex).Item _ ("Rate") = txtRate.Text ds2.Tables("Currency").Rows _ (ComboBox3.SelectedIndex).Item _ ("Rate") = txtRate.Text saveRate(ds2) End Sub

Figure 4. Using the Currency Converter. Users enter the amount to convert and the application retrieves the exchange rate from an XML file to perform the conversion.
Finally, the saveRate() method saves the new exchange rates onto file:

Public Sub saveRate(ByVal ds As DataSet) ds.WriteXml(FILENAME) MsgBox("Rates saved.") End Sub

That's it! Press F5 in Visual Studio to run the application. Figure 4 shows the currency converter in action.

A currency converter is just an example of types of things you can do with .NET CF. If a currency converter is not what you need, you can extrapolate the logic and tactics shown in this article in thousands of other ways. For example, you've learned:

  • How to use various built-in controls in .NET CF
  • How to use Datasets to manipulate XML documents
  • How to use of XML documents as data storage
  • How to program your controls to handle events

In my next article for DevX, I will show how to build an enhanced file explorer using the TreeView control. It will emulate the Windows Explorer that users are used to in Windows.



Wei-Meng Lee is a Microsoft .NET MVP and co-founder of Active Developer, a technology company specializing in hands-on training on the latest technologies. He is an established developer and trainer specializing in .NET and wireless technologies. He speaks and writes frequently on topics ranging from .NET to Mac OS X. He is the author of "Windows XP Unwired" (OReilly & Associates) and is currently working on "Programming the .NET Compact Framework," also from OReilly. He can be reached at weimeng@activedevelop.com.
Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date