WebserviceX.net offers a public Currency Converter SOAP Web Service with 149 different currencies. In a WinForms form I setup a pair of ComboBoxes for To and From currencies listing the supported currencies. Unfortunately there isn’t a Web Service providing the currencies so I populate the pull down from a Here-String loaded with a list copied off the service description site.

Add-Type -AssemblyName System.Windows.Forms
# Main Form
$mainForm = New-Object System.Windows.Forms.Form
$mainForm.Font = "Comic Sans MS,10"
$mainForm.Text = " Currency Converter"
$mainForm.ForeColor = "White"
$mainForm.BackColor = "DarkBlue"
$mainForm.Width = 600
$mainForm.Height = 170

# From Input Label
$fromInputLabel = New-Object System.Windows.Forms.Label
$fromInputLabel.Location = "45,10"
$fromInputLabel.Height = 22
$fromInputLabel.Width = 100
$fromInputLabel.Text = "From Currency"
$mainForm.Controls.Add($fromInputLabel)

# From Input ComboBox
$fromComboBox = New-Object System.Windows.Forms.ComboBox
$fromComboBox.Location = "145,10"
$fromComboBox.Size = "300,20"
$fromComboBox.ForeColor = "MediumBlue"
$fromComboBox.BackColor = "White"
# Add items to Combo box
for ($i=0; $i -lt $currencies.Count; $i++) {
    [void]$fromComboBox.items.add($currencies[$i])
}
$fromComboBox.SelectedIndex = 138;
$mainForm.Controls.Add($fromComboBox)

$currencies = @(
    ("Afghanistan Afghani - AFA")
    ("Albanian Lek - ALL")
    ("Algerian Dinar - DZD")
    ("Argentine Peso - ARS")
    .
    .
    .
    ("Yugoslav Dinar - YUM")
    ("Zambian Kwacha - ZMK")
    ("Zimbabwe Dollar - ZWD")
)

After selecting the currencies, you click the Lookup button to get the conversion rate. The button calls the CurrencyConverter function. The SOAP request ConversionRate is placed with the service provider. The returned XML is data only and is placed in the output string formatted to report on the conversion between the to and from currencies.

# Converter Button
$converterButton = New-Object System.Windows.Forms.Button
$converterButton.Location = "470,10"
$converterButton.Size = "75,28"
$converterButton.ForeColor = "DarkBlue"
$converterButton.BackColor = "White"
$converterButton.Text = "Convert"
$converterButton.add_Click({CurrencyConverter})
$mainForm.Controls.Add($converterButton)

Function CurrencyConverter {
    $from = $fromComboBox.Text.Substring(($fromComboBox.Text.Length - 3),3)
    $to = $toComboBox.Text.Substring(($toComboBox.Text.Length - 3),3)
    $currencyConvertor = New-WebServiceProxy -uri "http://www.webservicex.net/CurrencyConvertor.asmx?WSDL"
    $selection = $currencyConvertor.ConversionRate($from,$to)
    $formattedOutput = "One " + $fromComboBox.Text.Substring(0,($fromComboBox.Text.Length - 5)) + "is worth " + `
			$selection + " " + $toComboBox.Text.Substring(0,($toComboBox.Text.Length - 5))
    $convertOutputBox.Text = $formattedOutput

}

The formatted output string is passed to the output TextBox.

# Currency Output Label
$convertOuputLabel = New-Object System.Windows.Forms.Label
$convertOuputLabel.Location = "45,80"
$convertOuputLabel.Height = 22
$convertOuputLabel.Width = 100
$convertOuputLabel.Text = "Converted"
$mainForm.Controls.Add($convertOuputLabel)

# Currency TextBox
$convertOutputBox = New-Object System.Windows.Forms.TextBox
$convertOutputBox.Location = "145,80"
$convertOutputBox.Size = "400,23"
$convertOutputBox.ForeColor = "MediumBlue"
$convertOutputBox.BackColor = "White"
$mainForm.Controls.Add($convertOutputBox)

CurrencyConverter

CurrencyConverter.ps1

Advertisements