title: Calculating Profit in Visual Basic
body: <p>This program is supposed to read in values from a text file and get a sum of all these values. It is then to use information gathered from a series of check boxes and text boxes to calculate the final profit. </p>
  
  <p>As the code is written now, the profit is only correct if all check boxes are selected, but I need it to be correct if one, two, or all three are checked. Here is the current code
      Option Strict On</p>
  
  <pre><code>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(("$" &amp; Val(sum) - (Val(txtWages.Text) + Val(txtRent.Text) + Val(txtUtilities.Text))))
          End If
          If chkRent.Checked Then
              profit = CDbl(("$" &amp; Val(sum) - (Val(txtRent.Text) + Val(txtWages.Text) + Val(txtUtilities.Text))))
          End If
          If chkUtilities.Checked Then
              profit = CDbl(("$" &amp; Val(sum) - (Val(txtUtilities.Text) + Val(txtWages.Text) + Val(txtRent.Text))))
          End If
  
          txtAnswer.Text = profit.ToString
  
      End Sub
  End Class
  </code></pre>
  
  <p>Any help would be greatly appreciated.</p>