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 != ""

そのテキストが "" の場合、double に変換できません

于 2013-12-11T14:50:41.957 に答える