1

当我尝试“清除”程序中的文本框时,我遇到了以下问题。这是一个初学者的课程,所以我应该能够用我到目前为止所学的内容来回答修复错误。每当我按 NextYear 时,该程序基本上都会计算利率和资本增长。当我按下“清除”按钮时,它会显示错误并将我指向事件capital = CDbl(txtInitialCap.Text)下的语法。txtRate_TextChanged

Private year As Integer
Private capital As Double

Private Sub btnNextYear_Click(sender As Object, e As EventArgs) Handles btnNextYear.Click
    Dim interestAmount, interestRate As Double
    '
    ' Assign value to InterestRate variable from text property
    '
    interestRate = CDbl(txtRate.Text)
    '
    ' Calculate new values for year, interest amount and capital
    '
    year = year + 1
    interestAmount = capital * interestRate
    capital = capital + interestAmount
    '
    ' Assign the values to the textboxes
    '
    txtYear.Text = year
    txtInterest.Text = interestAmount
    txtCapital.Text = capital


Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        txtInitialCap.Text = vbNullString
        txtRate.Text = vbNullString
        txtYear.Text = vbNullString
        txtInterest.Text = vbNullString
        txtCapital.Text = vbNullString
    End Sub

Private Sub txtInitialCap_LostFocus(sender As Object, e As EventArgs) Handles txtInitialCap.LostFocus
    '
    ' Reinitialise the capital variable since probably it was changed
    '
    capital = CDbl(txtInitialCap.Text)
End Sub

Private Sub txtInitialCap_TextChanged(sender As Object, e As EventArgs) Handles txtInitialCap.TextChanged
    '
    ' Reinitialise the year variable and the textboxes
    '
    year = 0
    txtYear.Text = vbNullString
    txtInterest.Text = vbNullString
    txtCapital.Text = vbNullString
End Sub

Private Sub txtRate_TextChanged(sender As Object, e As EventArgs) Handles txtRate.TextChanged
    year = 0
    capital = CDbl(txtInitialCap.Text)
    txtYear.Text = vbNullString
    txtInterest.Text = vbNullString
    txtCapital.Text = vbNullString
End Sub
4

1 回答 1

0

我知道这是一个旧帖子。我会回答以防其他人发现这个。在 txtRate_TextChanged 中,您需要检查

txtInitialCap.Text != ""

如果文本是“”,则不能将其转换为双精度

于 2013-12-11T14:50:41.957 回答