简体   繁体   English

程序崩溃的Visual Basic

[英]Program crashing Visual Basic

I am programming in Visual Basic and i have a program created that is a loan calculator. 我正在Visual Basic中编程,并且创建了一个是借贷计算器的程序。 I pretty much have everything correct and it works and operates almost the way i want it too. 我几乎一切都正确,它几乎可以按照我想要的方式工作和运行。

Here is my code so far: 到目前为止,这是我的代码:

Public Class Form1 公开课表格1

Private MinAmt As Decimal = 1000
Private MaxAmt As Decimal = 200000
Private Drate As Decimal
Private Loan As Decimal

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    '== Load the Rate and Terms combo, then 'click Reset
    For r As Decimal = 0 To 0.1475 Step 0.0025
        '== Add each successive value to the combo list
        cbxRate.Items.Add(Format(r, "00.00%"))
    Next
    For t As Byte = 12 To 120 Step 6
        cbxTerm.Items.Add(Format(t, "00"))
    Next
    btnReset.PerformClick()
End Sub

Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
    '== Set the defaults, and clear my prior answers
    cbxRate.Text = "06.00%"
    cbxTerm.Text = "36"
    tbxLoan.Text = "10,000"
    lblPayment.Text = ""
End Sub

Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
    '== Validate the loan amount (number 1,000 - 200,000);

    If IsNumeric(tbxLoan.Text) AndAlso tbxLoan.Text >= MinAmt AndAlso tbxLoan.Text <= MaxAmt Then
        Loan = tbxLoan.Text
    Else
        MessageBox.Show("Error invalid value")
        tbxLoan.Focus()
        tbxLoan.SelectAll()
    End If

    '== Calculate Monthly Payment as PMT(monthly decimal rate, term in months, minus amount)
    lblPayment.Text = Pmt(cbxRate.Text.Replace("%", "") / 1200, cbxTerm.Text, -tbxLoan.Text)

    '== Display formatted as money: FormatCurrency(blah blah blah)
    lblPayment.Text = FormatCurrency(lblPayment.Text, 2)
End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    Close()

End Sub

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
    '== Confirm he means it, else cancel attempt to close.
    If MessageBox.Show("Do you really want to exit?", "Close", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
    Else
        e.Cancel = True
    End If
End Sub

End Class 最终班

I tried to post a picture of my program so it could be easier, but apparently i don't have enough reputation points, but ill try to explain the best i can. 我试图发布程序的图片,这样可能会更容易,但是显然我没有足够的声誉点,但是我试图解释我所能做到的最好。

I would like to eliminate the problem of someone accidentally putting a Letter as a value in the "Enter Loan Amount" space. 我想消除有人不小心将字母作为值输入“输入贷款金额”空间的问题。 Right now if i were to put a letter "U" in the "Enter Loan Amount" space it would pop up an error (which is what i want), but it also crashes the program (what i don't want). 现在,如果我要在“输入贷款金额”空间中输入字母“ U”,则会弹出一个错误(这是我想要的),但是也会使程序崩溃(我不想要的)。

Hopefully that makes sense. 希望这是有道理的。 Would anyone be able to point me in the right direction? 有人能指出我正确的方向吗? I am still new to Visual Basic. 我还是Visual Basic的新手。

Thank you! 谢谢!

It's because you're continue to calculate whatever or not the input was valid. 这是因为您将继续计算输入是否有效。

Exit the function by using Return after input was invalid. 输入无效后,通过使用Return退出功能。

Else
    MessageBox.Show("Error invalid value")
    tbxLoan.Focus()
    tbxLoan.SelectAll()
    Return
End If

or just move the calculations to inside the If statement 或只是将计算移至If语句中

Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click

    '== Validate the loan amount (number 1,000 - 200,000);
    If IsNumeric(tbxLoan.Text) AndAlso tbxLoan.Text >= MinAmt AndAlso tbxLoan.Text <= MaxAmt Then

        Loan = tbxLoan.Text

        '== Calculate Monthly Payment as PMT(monthly decimal rate, term in months, minus amount)

        lblPayment.Text = Pmt(cbxRate.Text.Replace("%", "") / 1200, cbxTerm.Text, -tbxLoan.Text)

        '== Display formatted as money: FormatCurrency(blah blah blah)
        lblPayment.Text = FormatCurrency(lblPayment.Text, 2)

    Else
        MessageBox.Show("Error invalid value")
        tbxLoan.Focus()
        tbxLoan.SelectAll()
    End If

End Sub

You are still executing the rest of the code. 您仍在执行其余代码。 You need to exit that block. 您需要退出该街区。 Make use of Return statement.. 利用Return语句。

Else
    MessageBox.Show("Error invalid value")
    tbxLoan.Focus()
    tbxLoan.SelectAll()
    Return
End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM