2014-02-15 55 views
0

我目前正在VB.NET中製作貨幣轉換器,但有一點障礙。我想要轉換的貨幣是在組合框中選擇的。想要在VB.NET中聲明一個字符串的值

現在,它有點類似於:

If Combobox1.Text = "USD" Then 
     USD1.Text = Amount.Text * (USD/USD) 
     EUR1.Text = Amount.Text * (USD/EUR) 
     GBP1.Text = Amount.Text * (USD/GBP) 
     NOK1.Text = Amount.Text * (USD/NOK) 
    End If 
    If Combobox1.Text = "EUR" Then 
     USD1.Text = Amount.Text * (EUR/USD) 
     EUR1.Text = Amount.Text * (EUR/EUR) 
     GBP1.Text = Amount.Text * (EUR/GBP) 
     NOK1.Text = Amount.Text * (EUR/NOK) 
    End If 

但我希望它看起來更像是這樣的:

USD1.Text = Amount * (SelectedExch/USD) 
    EUR1.Text = Amount * (SelectedExch/EUR) 

所以有什麼建議?

編輯:我做了一些研究和試驗,並在這裏是我目前的代碼:

Private Sub Textbox1_TextChanged(sender As Object, e As EventArgs) Handles Amount.TextChanged 
    Dim USD = My.Settings.USD 
    Dim EUR = My.Settings.EUR 
    Dim GBP = My.Settings.GBP 
    Dim NOK = My.Settings.NOK 
    Dim value, value2 As Decimal 
    If Not Decimal.TryParse(Amount.Text, value) Then 
     USD1.Text = "" 
     EUR1.Text = "" 
     GBP1.Text = "" 
     NOK1.Text = "" 
     Return 
    End If 
    Select Case Currency.Text 
     Case "USD" 
      value2 = USD 
     Case "EUR" 
      value2 = EUR 
     Case "GBP" 
      value2 = GBP 
     Case "NOK" 
      value2 = NOK 
    End Select 
    value2 *= value 
    USD1.Text = Math.Round((value2/USD), 1) 
    EUR1.Text = Math.Round((value2/EUR), 1) 
    GBP1.Text = Math.Round((value2/GBP), 1) 
    NOK1.Text = Math.Round((value2/NOK), 1) 
End Sub 

我不能說我理解的代碼的一切。你看到有改進的餘地嗎?後來我想給用戶選擇增加貨幣

回答

0

這裏或許要做到這一點

Public Class Form1 

    Private Enum CurrencyTypes 
     USD 
     EUR 
     GBP 
     NOK 
    End Enum 

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) _ 
                  Handles MyBase.Load 
     ComboBox1.DataSource = [Enum].GetValues(GetType(CurrencyTypes)) 
    End Sub 

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) _ 
              Handles ComboBox1.SelectedIndexChanged 
     Dim source As CurrencyTypes = [Enum].Parse(GetType(CurrencyTypes), _ 
                ComboBox1.SelectedValue) 
     UpdateConversion(source) 
    End Sub 

    Private Sub UpdateConversion(ByVal sourceCurrency As CurrencyTypes) 
     USD1.Text = ConvertCurrency(sourceCurrency, CurrencyTypes.USD).ToString() 
     EUR1.Text = ConvertCurrency(sourceCurrency, CurrencyTypes.EUR).ToString() 
     GBP1.Text = ConvertCurrency(sourceCurrency, CurrencyTypes.GBP).ToString() 
     NOK1.Text = ConvertCurrency(sourceCurrency, CurrencyTypes.NOK).ToString() 
    End Sub 

    Private Function ConvertCurrency(ByVal fromType As CurrencyTypes, _ 
              toType As CurrencyTypes) As Decimal 
     'do conversion 
    End Function 

End Class 

一個稍微更優雅的方式您還沒有表現出你是如何讓你轉換值的任何實現細節。你似乎在絃樂上顯示數學,這沒有任何意義 - 如果你關掉了Option Strict,那麼爲了愛好所有事情,請把它打開。無論如何,我還沒有填寫ConvertCurrency函數,但大概你是從某處獲取交換值。所有該功能需要做的是將轉換值與CurrencyTypes枚舉類型關聯。如果你不知道如何做到這一點,這可能是另一個問題的一個很好的主題。

+0

好,貨幣值只能手動寫入,並且可以由用戶編輯。我希望程序能夠在稍後從互聯網資源中獲得利率,但我目前不知道如何去做。如果您想查看,我已經在頂端文章中編輯了代碼。 – Commodent

0

......怎麼

Public Class Form1 

    Dim lstExchangeRates As New List(Of ExchangeRate) 

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 

     Dim excExchangeRate As ExchangeRate 

     excExchangeRate = New ExchangeRate With {.RateName = "USD", .Rate = 1} 
     lstExchangeRates.Add(excExchangeRate) 

     excExchangeRate = New ExchangeRate With {.RateName = "EUR", .Rate = 1.2} 
     lstExchangeRates.Add(excExchangeRate) 

     excExchangeRate = New ExchangeRate With {.RateName = "GBP", .Rate = 0.5} 
     lstExchangeRates.Add(excExchangeRate) 

     excExchangeRate = New ExchangeRate With {.RateName = "NOK", .Rate = 1.5} 
     lstExchangeRates.Add(excExchangeRate) 

     ComboBox1.DataSource = lstExchangeRates 
     ComboBox1.DisplayMember = "RateName" 
     ComboBox1.ValueMember = "Rate" 

    End Sub 

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged 
     CalculateValues() 

    End Sub 

    Private Sub Amount_TextChanged(sender As Object, e As EventArgs) Handles Amount.TextChanged 
     CalculateValues() 

    End Sub 

    Private Sub CalculateValues() 

     If Amount.Text = "" Then Exit Sub 

     For Each ExchangeRateItem As ExchangeRate In lstExchangeRates 

      Dim txtExchangeRate As TextBox = CType(Me.Controls(ExchangeRateItem.RateName & "1"), TextBox) 

      txtExchangeRate.Text = CDbl(Amount.Text) * (ComboBox1.SelectedValue/(From ExchangeRates As ExchangeRate In lstExchangeRates 
              Where ExchangeRates.RateName = ExchangeRateItem.RateName 
              Select ExchangeRates.Rate).FirstOrDefault) 

     Next 

    End Sub 

End Class 

Public Class ExchangeRate 

    Public Property RateName As String 
    Public Property Rate As Double 

End Class 
0

爲什麼不最簡單的方法,用字典的表格裝入建設:

Dim Currencies as New Dictionary(Of String, Double) 
Currencies.Add("USD", [Value of USD]) 
Currencies.Add("EUR", [Value of EUR]) 
... 

,並呼籲只是想:

Dim SelectedExch as Double = Currencies(Combobox1.Text) 
USD1.Text = Amount.Text * (SelectedExch/USD) 
EUR1.Text = Amount.Text * (SelectedExch/EUR) 
GBP1.Text = Amount.Text * (SelectedExch/GBP) 
NOK1.Text = Amount.Text * (SelectedExch/NOK) 
相關問題