Learning the .NET Framework: Project A Currency Changer - Populating the Menu
(Page 2 of 3 )
Besides adding the various controls, you must add a Menu-Item control to the MainMenu control, as shown in Figure 3-4. When the user selects the Clear History menu item, it clears the ListBox control in the first tab page.

Figure 3-4. Adding the MenuItem control to the MainMenu
Coding the Controls Now that the controls are set up, you need to begin writing the application logic that performs the currency translation. This application deals with three currencies and uses a single dataset object to bind the exchange rate of each currency to the ComboBox controls, where the end user selects the currencies to convert. To do this, switch to code view and declare the following DataSet object and a global Boolean variable:
Dim ds As New DataSet
Dim ready As Boolean = False
Now you need to tell the application where to get the exchange rate information. Rates.xml contains the exchange rate for each currency:
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 object using three different DataView objects to the three ComboBox controls:
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
LoadRates(ds)
Dim viewFrom As New _
DataView(ds.Tables("Currency"))
Dim viewTo As New _
DataView(ds.Tables("Currency"))
Dim viewRate As New _
DataView(ds.Tables("Currency"))
ComboBox1.Items.Clear()
ComboBox1.DataSource = viewFrom
ComboBox1.DisplayMember = "Sym"
ComboBox1.ValueMember = "Rate"
ComboBox2.Items.Clear()
ComboBox2.DataSource = viewTo
ComboBox2.DisplayMember = "Sym"
ComboBox2.ValueMember = "Rate"
ComboBox3.Items.Clear()
ComboBox3.DataSource = ViewRate
ComboBox3.DisplayMember = "Sym"
ComboBox3.ValueMember = "Rate"
ready = True
End Sub
The LoadRates( ) method tries to load the exchange rate information from the file Rates.xml, shown here:
<?xml version=1.0 standalone=yes?>
<NewDataSet>
<Currency>
<Sym>USD</Sym>
<Rate>1</Rate>
</Currency>
<Currency>
<Sym>CNY</Sym>
<Rate>8</Rate>
</Currency>
<Currency>
<Sym>SGD</Sym>
<Rate>1.74</Rate>
</Currency>
</NewDataSet>
The base currency is the U.S. dollar. All other currencies are converted relative to the U.S. dollar. For example, $1 U.S. is equivalent to about 1.69 Singapore dollars. Here is the code for the LoadRates() subroutine:
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 rates file does not exist when the application is first loaded, so the InitRates( ) method loads the exchange rates from a currency converter web service, such as the one located at http://www.webservicex.com/CurrencyConvertor.asmx. To use this web service—which converts the major currencies in the world—add a Web Reference to your project by right-clicking on your project name in Solution Explorer and then selecting Add Web Reference. The default proxy class name for this web service created by Visual Studio .NET is called com.webservicex.www.
The InitRates() method creates a dataset containing the currency table and three exchange rates obtained from the currency converter web service. If the web service is not reachable, a default exchange rate is used. The information is then written to file:
Public Sub InitRates(ByVal ds As DataSet)
Dim ws As New _
com.webservicex.www.CurrencyConvertor
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") = "USD"
row("Rate") = 1
ds.Tables("Currency").Rows.Add(row)
row = ds.Tables("Currency").NewRow
row("Sym") = "CNY"
Try
row("Rate") = ws.ConversionRate( _
com.webservicex.www.Currency.USD, _
com.webservicex.www.Currency.CNY)
Catch ex As Exception
MsgBox("Error contacting web " & _
"service. Setting to default " & _
"rate.")
row("Rate") = 8
Finally
ds.Tables("Currency").Rows.Add(row)
End Try
row = ds.Tables("Currency").NewRow
row("Sym") = "SGD"
Try
row("Rate") = ws.ConversionRate( _
com.webservicex.www.Currency.USD, _
com.webservicex.www.Currency.SGD)
Catch ex As Exception
MsgBox("Error contacting web service." & _
" Setting to default rate.")
row("Rate") = 1.74
Finally
ds.Tables("Currency").Rows.Add(row)
End Try
ds.WriteXml(FILENAME)
End Sub
Next: Accepting Data Entry >>
More .NET Articles
More By O'Reilly Media
|
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.
|
|