在Visual Basic中计算利润

时间:2014-02-28 08:52:04

标签: vb.net

该程序应该从文本文件读取值并获得所有这些值的总和。然后使用从一系列复选框和文本框收集的信息来计算最终利润。

现在编写代码时,只有选中所有复选框才能获得利润,但如果检查了一个,两个或全部三个复选框,我需要它是正确的。这是当前的代码     Option Strict On

Imports System.IO

Public Class Form1

    Dim sum As Double
    Dim fileRead As Boolean
    Dim profit As Double

    Private Sub menOpen_Click(sender As Object, e As EventArgs) Handles menOpen.Click
        Dim ofd As New OpenFileDialog
        ofd.Filter = "text files |*.txt|All Files|*.*"
        ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)

        If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim selectedFileName As String = System.IO.Path.GetFileName(ofd.FileName)
            If selectedFileName.ToLower = "profit.txt" Then
                Dim line As String
                Using reader As New StreamReader(ofd.OpenFile)
                    While Not reader.EndOfStream
                        line = reader.ReadLine
                        Dim value As Integer
                        If Integer.TryParse(line, value) Then
                            sum = sum + value
                            fileRead = True
                        End If
                        Console.WriteLine(line)
                    End While
                End Using
            Else
                MessageBox.Show("You cannot use that file!")
            End If
        End If
    End Sub

    Private Sub menExit_Click(sender As Object, e As EventArgs) Handles menExit.Click
        Me.Close()
    End Sub

    Private Sub radSales_CheckedChanged(sender As Object, e As EventArgs) Handles radSales.CheckedChanged
        If radSales.Checked Then
            profit = sum
        End If
    End Sub

    Private Sub radSandO_CheckedChanged(sender As Object, e As EventArgs) Handles radSandO.CheckedChanged
        If radSandO.Checked Then
            If Trim(txtWages.Text) = "" Then
               txtWages.Text = CStr(0)
            End If
            profit = (sum - CDbl(txtWages.Text) - CDbl(txtRent.Text) - CDbl(txtUtilities.Text))
        End If

    End Sub


    Private Sub menComputeProfit_Click(sender As Object, e As EventArgs) Handles menComputeProfit.Click
        If fileRead = False Then
            MessageBox.Show("The file profit.txt has not been read in yet, the profit will be set to zero.")
            sum = 0
        End If

        If chkWages.Checked Then
             profit = CDbl(("$" & Val(sum) - (Val(txtWages.Text) + Val(txtRent.Text) + Val(txtUtilities.Text))))
        End If
        If chkRent.Checked Then
            profit = CDbl(("$" & Val(sum) - (Val(txtRent.Text) + Val(txtWages.Text) + Val(txtUtilities.Text))))
        End If
        If chkUtilities.Checked Then
            profit = CDbl(("$" & Val(sum) - (Val(txtUtilities.Text) + Val(txtWages.Text) + Val(txtRent.Text))))
        End If

        txtAnswer.Text = profit.ToString

    End Sub
End Class

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:0)

其中一个文本输入很可能为空(如错误状态)。即使它不是空的,它仍然可能是无效的Double值。

为了安全地检查字符串是否可以转换为double,您可以使用Double.TryParse,如下所示:

If Double.TryParse(value, number) Then
  Console.WriteLine("'{0}' --> {1}", value, number)
Else
  Console.WriteLine("Unable to parse '{0}'.", value)      
End If    

答案 1 :(得分:0)

在将字符串转换为double之前,请检查该字符串是否为空。这就是错误所说的。之后你应该检查框中的字符串是否真的是数字。

答案 2 :(得分:0)

您正在使用TextChanged事件进行txtAnswer

 Private Sub txtAnswer_TextChanged(sender As Object, e As EventArgs) Handles txtAnswer.TextChanged
    txtAnswer.Text = CStr(profit)
End Sub

所以上面的代码每次发生变化时都会自动激活!只需将利润计入txt每次计算时回答

If chkWages.Checked Then
        profit = CDbl(("$" & Val(sum) - Val(txtWages.Text)))
        txtAnswer.text = profit
    End If
    If chkRent.Checked Then
        profit = CDbl(("$" & Val(sum) - Val(txtRent.Text)))
        txtAnswer.text = profit
    End If
    If chkUtilities.Checked Then
        profit = CDbl(("$" & Val(sum) - Val(txtUtilities.Text)))
        txtAnswer.text = profit
    End If

有点不整洁,但应该这样做......每当利润变化时你都需要这样做。并摆脱TextChanged事件。

明确更新的答案

好的,我从不添加货币符号,所以我不确定CDbl(("$" & Val(Sum) - Val(txtWages.Text)))会做什么,因为它可能会将其视为字符串字符“$”而不是货币。 摆脱“$”并只添加值...如果你想在计算完成后将$坚持在前面说txtAnswer = $ & profit.toString