为什么我的程序没有显示答案? (视觉工作室)

时间:2014-11-21 11:15:35

标签: vb.net

大家好!

我目前正在制作转换货币的初学者计划。具体来说,美元和英镑。用户输入一个值,并根据他们选择的开始和结束货币,在标签中提供答案。问题是,答案不会显示在标签中。我已附上该代码以供参考。我尝试了一些事情,但我无法得到它。我确定你会对这段代码感到畏缩,所以除了回答我的问题之外,请随时给我其他建议。

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

结束班

2 个答案:

答案 0 :(得分: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

它与评论中提到的类似,但语法略有不同。

答案 1 :(得分: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

希望这有帮助