2014-11-21 29 views
0

大家好!爲什麼我的程序不顯示答案? (Visual Studio)

我目前正在爲貨幣轉換做一個初學者程序。特別是美元和英鎊。用戶輸入一個值,並根據他們選擇的開始和結束貨幣,在標籤中提供答案。問題是,答案不會顯示在標籤中。我附上了代碼以供參考。我嘗試了幾件事,但我無法得到它。我相信你們都會對這個代碼感到畏懼,所以隨時給我其他的建議,而不是我的問題的答案。

Dim result As Decimal 

Private Sub bt_run_Click(sender As Object, e As EventArgs) Handles bt_run.Click 
    If cb_start.SelectedValue = "Pound Sterling" And cb_end.SelectedValue = "Pound Sterling" Then 
     result = txt_amount.Text 
     lbl_ans.Text = result 
    ElseIf cb_start.SelectedValue = "Pound Sterling" And cb_end.SelectedValue = "US Dollar" Then 
     result = txt_amount.Text * 1.57 
     lbl_ans.Text = result 
    ElseIf cb_start.SelectedValue = "US Dollar" And cb_end.SelectedValue = "US Dollar" Then 
     result = txt_amount.Text 
     lbl_ans.Text = result 
    ElseIf cb_start.SelectedValue = "US Dollar" And cb_end.SelectedValue = "Pound Sterling" Then 
     result = txt_amount.Text * 0.64 
     lbl_ans.Text = result 
    End If 

End Sub 

末級

+0

調試你的代碼一步一步的,看看有什麼cb_start.SelectedValue'和'cb_end.SelectedValue'的'值實際上是。他們可能不是你所期待的。 – 2014-11-21 11:20:29

+0

您是否嘗試過.SelectedText而不是.SelectedValue?請將Option Strict設置爲ON。它會讓你的生活更輕鬆,更大的項目。 – 2014-11-21 11:36:57

回答

0

試試這個:

Dim result As Decimal 

Private Sub bt_run_Click(sender As Object, e As EventArgs) Handles bt_run.Click 
    If cb_start.ddlType.SelectedItem.Text = "Pound Sterling" And cb_end.ddlType.SelectedItem.Text = "Pound Sterling" Then 
     result = txt_amount.Text 
     lbl_ans.Text = result 
    ElseIf cb_start.ddlType.SelectedItem.Text = "Pound Sterling" And cb_end.ddlType.SelectedItem.Text = "US Dollar" Then 
     result = txt_amount.Text * 1.57 
     lbl_ans.Text = result 
    ElseIf cb_start.ddlType.SelectedItem.Text = "US Dollar" And cb_end.ddlType.SelectedItem.Text = "US Dollar" Then 
     result = txt_amount.Text 
     lbl_ans.Text = result 
    ElseIf cb_start.ddlType.SelectedItem.Text = "US Dollar" And cb_end.ddlType.SelectedItem.Text = "Pound Sterling" Then 
     result = txt_amount.Text * 0.64 
     lbl_ans.Text = result 
    End If 

End Sub 

這類似於在評論中提到,但語法是有點不同。

0

我相信你的問題實際上是與你一起工作的數據類型。

txt_amount.Text 'This is a string value that is returned. 

嘗試先將字符串轉換爲小數先進行數學運算。

CDec(txt_amount.Text) * 1.57 
'And for the other... 
CDec(txt_amount.Text) * 0.64 

編輯: 當把十進制值回你的文本框,你應該將其轉換回字符串。

lbl_ans.Text = (CDec(txt_amount.Text) * 1.57).ToString() 

編輯2:以下鏈接可能對您有用。這是有關數據類型轉換的Microsoft文檔。

Visual Basic Data-Type Conversion

希望這有助於

相關問題