Lab: Creating a Currency Converter

Lab: Creating a Currency Converter

In this lab, you ll create a simple currency converter Web application that uses the user s culture to determine the type of conversion to perform:

  • An unrecognized culture converts euros to dollars and pesos, and displays text in English as the fallback interface.

  • An English-US culture converts dollars to euros and pesos and displays an English-language interface.

  • A Spanish-Mexican culture converts pesos to dollars and euros and displays a Spanish-language interface.

When complete, the Web application will appear as shown in Figure 15-11.

figure 15-11 currency converter (fallback interface)

Figure 15-11. Currency converter (fallback interface)

Estimated lesson time: 20 minutes

Exercise 1: Create the Currency Converter Web Form

The currency converter Web application contains a single Web form that receives input from a text box and displays output in two labels when the user clicks OK. The Web form also includes a Cancel HTML button that clears the text box.

Because the application will be localized, the HTML elements on the Web form must have id and runat attributes so that the localized resource strings can be loaded into those controls from server-side code.

To create the Currency Converter Web form

  1. Create a new Web form named Converter.

  2. Switch to HTML view, and enter the following HTML between the <form> and </ form> tags of the Web form:

    <h1  runat="server">Head1</h1> <span  runat="server">Enter an amount: </span> <asp:TextBox  Runat="server" /> <br> <span  runat="server">Amount in: </span> <asp:Label  Runat="server" /> <br> <span  runat="server">Amount in: </span> <asp:Label  Runat="server" /> <br><br> <asp:Button  Text="OK" Runat="server" /> <input  runat="server" type="button"  onclick="txtCurrency.Text=''" value="Cancel">

  3. When you have finished, switch the Web form back to Design view. This is an important step because it generates the correct control declarations in the code file.

Exercise 2: Create the User-Interface Resource Files

The Web form created in the preceding exercise contains only dummy entries for the text that will actually be displayed in the user interface. For instance, the Web form s heading is Head1. In this exercise, you ll create the strings that are displayed in place of those dummy entries at run time.

To create the user-interface resource files

  1. From the Project menu, choose Add New Item, and then select Assembly Resource File from the list of templates. Name the resource file strings.resx.

  2. Add the entries shown in Figure 15-12 to the resource file to create the fallback values for the user interface.

    figure 15-12 fallback values for the user interface

    Figure 15-12. Fallback values for the user interface

  3. Create a new resource file named strings.en-US.resx, and add the entries shown in Figure 15-13 to create the interface for users with the English-US culture:

    figure 15-13 english-us culture values

    Figure 15-13. English-US culture values

  4. Create another new resource file named strings.es-MX.resx, and add the entries shown in Figure 15-14 for the Spanish-Mexican culture:

    figure 15-14 spanish-mexican culture values

    Figure 15-14. Spanish-Mexican culture values

  5. When you have finished, click Save All to save your work.

You can create additional resource files for other cultures if you want. Just include the culture code in the resource file name, as shown in the preceding steps. Visual Studio .NET will automatically compile the resources as satellite assemblies and store them in the appropriate subfolder when you build the application.

Exercise 3: Load Resources Based on User Culture

In this exercise, you ll detect the user s culture, load resources, and display the resources matching the user s culture.

To load resources based on the user s culture

  1. Add the following Imports or using statements to the beginning of the Web form s code file:

    Visual Basic .NET

    Imports System.Resources Imports System.Globalization Imports System.Threading

    Visual C#

    using System.Resources; using System.Globalization; using System.Threading;

  2. Add the following code at the class level of the Web form to load the resources into the ResourceManager object. Replace the projectName placeholder in the following code with the name of your application s root namespace.

    Visual Basic .NET

    Protected gStrings As New ResourceManager("projectName.strings", _ GetType(Converter).Assembly)

    Visual C#

    protected ResourceManager gStrings = new ResourceManager("projectName.strings", typeof(Converter).Assembly);

  3. Add the following code to the Page_Load event procedure to detect the user s culture, set the application thread s user interface culture to match, and display the localized resource strings in the user interface:

    Visual Basic .NET

    Private Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load If Not IsPostBack Then ' Get the user's preferred language. Dim sLang As String = Request.UserLanguages(0) ' Set the thread's UICulture to load resources. Thread.CurrentThread.CurrentUICulture = New CultureInfo(sLang) ' Set the thread's culture for date/string/currency formats. Thread.CurrentThread.CurrentCulture = _ CultureInfo.CreateSpecificCulture(sLang) ' Get strings from resource file. head1.InnerHtml = gStrings.GetString("converter.head1") sp1.InnerHtml = gStrings.GetString("converter.sp1") sp2.InnerHtml = gStrings.GetString("converter.sp2") sp3.InnerHtml = gStrings.GetString("converter.sp3") butOK.Text = gStrings.GetString("converter.butOK") butCancel.Value = gStrings.GetString("converter.butCancel") End If End Sub

    Visual C#

    private void Page_Load(object sender, System.EventArgs e) { if (!IsPostBack) { // Get the user's preferred language. string sLang = Request.UserLanguages[0]; // Set the thread's UICulture to load resources //from satellite assembly. Thread.CurrentThread.CurrentUICulture = new CultureInfo(sLang); // Set the thread's culture for date/string/currency formats. Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(sLang); // Get strings from resource file. head1.InnerHtml = gStrings.GetString("converter.head1"); sp1.InnerHtml = gStrings.GetString("converter.sp1"); sp2.InnerHtml = gStrings.GetString("v.sp2"); sp3.InnerHtml = gStrings.GetString("v.sp3"); butOK.Text = gStrings.GetString("v.butOK"); butCancel.Value = gStrings.GetString("v.butCancel"); } }

You have to load the localized user-interface strings only the first time the page is displayed, as shown in the preceding code. ASP.NET retains the value of the controls between page displays automatically.

Exercise 4: Perform Culture-Dependent Conversion

Just as the currency converter application displays a different interface for different cultures, it also performs different conversions based on the current culture. In this exercise, you ll write the code that performs the culture-dependent conversions.

To perform the culture-dependent conversions

Add the following code to the butOK_Click event procedure to detect the current culture and perform different conversions based on that information:

Visual Basic .NET

Private Sub butOK_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles butOK.Click ' Get the amount. Dim SrcAmount As Double = Convert.ToDouble(txtCurrency.Text) Dim DestAmount As Double ' Culture variables for currency formatting. Dim Europe As New CultureInfo("fr-FR") Dim USA As New CultureInfo("en-US") Dim Mexico As New CultureInfo("es-MX") ' Perform conversions based on current user-interface culture. Select Case Thread.CurrentThread.CurrentCulture.Name Case "en-US" ' Convert from dollars. DestAmount = SrcAmount * 1.1329 lblCurrency1.Text = DestAmount.ToString("C", Europe) DestAmount = SrcAmount * 9.0655 lblCurrency2.Text = DestAmount.ToString("C", Mexico) Case "es-MX" ' Convert from pesos. DestAmount = SrcAmount * 0.125 lblCurrency1.Text = DestAmount.ToString("C", Europe) DestAmount = SrcAmount * 0.1103 lblCurrency2.Text = DestAmount.ToString("C", USA) Case Else ' Convert from euros. DestAmount = SrcAmount * 8.0021 lblCurrency1.Text = DestAmount.ToString("C", USA) DestAmount = SrcAmount * 0.1103 lblCurrency2.Text = DestAmount.ToString("C", Mexico) End Select End Sub

Visual C#

private void butOK_Click(object sender, System.EventArgs e) { // Get the amount. double SrcAmount = Convert.ToDouble(txtCurrency.Text); double DestAmount; // Culture variables for currency formatting. CultureInfo Europe = new CultureInfo("fr-FR"); CultureInfo USA = new CultureInfo("en-US"); CultureInfo Mexico = new CultureInfo("es-MX"); // Perform conversions based on current user-interface culture. switch (Thread.CurrentThread.CurrentCulture.Name) { case "en-US": // Convert from dollars. DestAmount = SrcAmount * 1.1329; lblCurrency1.Text = DestAmount.ToString("C", Europe); DestAmount = SrcAmount * 9.0655; lblCurrency2.Text = DestAmount.ToString("C", Mexico); break; case "es-MX": // Convert from pesos. DestAmount = SrcAmount * 0.125; lblCurrency1.Text = DestAmount.ToString("C", Europe); DestAmount = SrcAmount * 0.1103; lblCurrency2.Text = DestAmount.ToString("C", USA); break; default: // Convert from euros. DestAmount = SrcAmount * 8.0021; lblCurrency1.Text = DestAmount.ToString("C", USA); DestAmount = SrcAmount * 0.1103; lblCurrency2.Text = DestAmount.ToString("C", Mexico); break; } }

The preceding code creates CultureInfo objects (Europe, United States, and Mexico) for each cultural conversion. These objects provide the appropriate currency formatting in the ToString methods. The Europe object uses the culture code for France, because France uses the euro as currency. There isn t a culture code for the European Union.

The preceding example also hard-codes the exchange rates into the procedure. You wouldn t do that in a real-world situation, of course. Getting current exchange rates is an appropriate task for Web Services. See Chapter 7, Advanced Web Forms Programming, for a lesson on how to use XML Web Services.



MCAD(s)MCSD Self-Paced Training Kit(c) Developing Web Applications With Microsoft Visual Basic. Net and Microsoft V[.  .. ]0-315
MCAD(s)MCSD Self-Paced Training Kit(c) Developing Web Applications With Microsoft Visual Basic. Net and Microsoft V[. .. ]0-315
ISBN: N/A
EAN: N/A
Year: 2003
Pages: 118

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net