vb.net 货币转换器 Visual Basic 2010

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19260041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:24:16  来源:igfitidea点击:

Currency Converter Visual basic 2010

vb.net

提问by JC3196

I'm here for a bit problem in dealing the currency conversion by manual.

我来这里是因为在手动处理货币转换时遇到了一些问题。

How to put another value on the same existed combobox which the answers must be different.

如何在相同的现有组合框上放置另一个值,答案必须不同。

Example Japan Yen to US Dollars = 12.43 but US Dollars to Japan Yen does have same answer.

示例 日元兑美元 = 12.43 但美元兑日元确实有相同的答案。

What is the way to put the values on combobox2 which the answers are always different when set.

将值放在 combobox2 上的方法是什么,设置时答案总是不同的。



 Private Sub CurrencyFromCombobox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CurrencyFromCombobox.SelectedIndexChanged


 Select Case CurrencyFromCombobox.Text

 Case "Japanese Yen - JPY"
            fromjapan = 1
        Case "U.S. Dollar - USD"
            fromUSdollars = 1
    End Select


 Private Sub CurrencyToCombobox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CurrencyToCombobox.SelectedIndexChanged
    Select Case CurrencyToCombobox.Text
        Case "Japanese YEN - JPY"
            japanto = 1.0
            USto = 323
        Case "Euro - EUR"
            japanto = 0.0075774548
        Case "British Pound - GBP"
            japanto = 0.0064142153
        Case "Indian Rupee - INR"
            japanto = 0.6309714918
        Case "Australian Dollar - AUD"
            japanto = 0.0108982954
        Case "U.S. Dollar - USD"
            japanto = 0.0102789194
        Case "Canadian Dollar - CAD"
            japanto = 0.010583474
        Case "UAE Dirham - AED"
            japanto = 0.0377534428

    End Select


    '------------------------------------------------------------Currency rates ffom US        Dollars
    Select Case CurrencyToCombobox.Text
        Case "Japanese YEN - JPY"
            USto = 323
        Case "Euro - EUR"
            USto = 0.7368
        Case "British Pound - GBP"
            USto = 0.6217
    End Select
End Sub

   Private Sub Enterbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Enterbutton.Click
    Dim enter As Decimal

    ' Japanese yen conversion
    enter = Val(Enteramounttxt.Text)
    result = enter * fromjapan * japanto
    Resulttxt.Text = result.ToString

    ' US Dollar conversion

    result1 = enter * fromUSdollars * USto
    Resulttxt.Text = result1.ToString
End Sub

回答by SSpoke

Try this (requires internet connection to work)

试试这个(需要互联网连接才能工作)

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox(Convert(0.5, "USD", "EUR"))
    End Sub

    Public Function Convert(ByVal amount As Decimal, ByVal fromCurrency As String, ByVal toCurrency As String) As Decimal

        Dim web As System.Net.WebClient = New System.Net.WebClient()
        Dim url As String = String.Format("http://www.google.com/ig/calculator?hl=en&q={2}{0}%3D%3F{1}", fromCurrency.ToUpper(), toCurrency.ToUpper(), amount)
        Dim response As String = web.DownloadString(url)

        Dim regex As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("rhs: \""(\d*.\d*\.?\d*)")
        Dim match As System.Text.RegularExpressions.Match = regex.Match(response)

        Dim rate As Decimal = System.Convert.ToDecimal(match.Groups(1).Value)
        Return rate
    End Function
End Class