Learning the .NET Framework: Project A Currency Changer - Accepting Data Entry
(Page 3 of 3 )
Users enter the amount of currency they wish to convert using the buttons 0 to 9 on the first tab page, which invokes the cmdButtons( ) event. Note that this event is fired whenever one of the 11 buttons (0 to 9 and the decimal point button) 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 (txtValue) to show the user which numbers she 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 currency to convert from).
The selection in ComboBox2 is changed (the currency to convert to). - The value in the TextBox control is changed.
Here is the UpdateValue() event:
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 invoked 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 tab page control for the sample application allows the user to change the exchange rate of each currency. In production, this would frequently be done via the web service, which would grab the latest rates. In this case, users can override the rates included in the Rates.xmlfile. When ComboBox3 control is selected, 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 = ds.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.xmlfile:
Private Sub cmdUpdate_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles cmdUpdate.Click
ds.Tables("Currency").Rows _
(ComboBox3.SelectedIndex).Item _
("Rate") = txtRate.Text
saveRate(ds)
End Sub
Finally, the saveRate() method saves the new exchange rates into the 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 3-5 shows the Currency Converter in action.
A currency converter is just an example of the types of things you can do with .NET Compact Framework. If a currency converter is not what you need, you can extrapolate the logic and tactics shown in this project in thousands of other ways. For example, you’ve learned:
- How to use various built-in controls in .NET Compact Framework
- How to use datasets to manipulate XML documents
- How to use web services to retrieve information
- How to use XML documents as data storage
How to program your controls to handle events
Figure 3-5. Using the Currency Converter
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is taken from chapter three of the book Programming the .NET Compact Framework, written by Wei-Meng Lee (O'Reilly, 2005; ISBN: 0596004338). Check it out at your favorite bookstore. Buy this book now.
|
|