I am having an issue with a DivideByZeroException error. When I click a check box, my program closes and I get the error. Here are my codes:
Also having issues with text file data not showing in the Histlist listbox when selection is made from the dropdown box. Any help with these will be greatly appreciated. Thanks
Code:
Option Strict On
Imports System.IO
Imports Microsoft.VisualBasic.FileIO
Public Class John_Everett_P3
Dim arrTableInfo(22, 3) As String
Dim arrHighLow(5, 1) As String
Dim IndexFieldParser As TextFieldParser
Dim decSC As Decimal
Dim decGL As Decimal
Dim decRM As Decimal
Dim decTD As Decimal
Dim decPB As Decimal
Dim decDC As Decimal
Private Sub John_Everett_P3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strPath As String
Dim intLineCount As Integer
Dim intFirstRec As Integer
Directionslbl.Text = "Program Displays the Box Office" & vbNewLine & _
"Select one of the agents to see all" & vbNewLine & _
"Movies and box office totals."
strPath = App_Path() & "Program 3 Data.txt"
'Try/catch if file exists read it in otherwise use hardcoded array
Try
'obtain linecount of file for the For loop
intLineCount = GetFileLineCountNew(strPath)
Dim strDataPath As New StreamReader(strPath)
'Setup file Read-in to account for Tabs
IndexFieldParser = New TextFieldParser(strDataPath)
IndexFieldParser.TextFieldType = Delimited
IndexFieldParser.SetDelimiters(vbTab)
'Begin to Read info from file and store in multidimensional Array.
Dim FieldString() As String
Dim intcolumn As Integer
For intFirstRec = 0 To intLineCount - 1
'Read in delimited line split by tab
FieldString = IndexFieldParser.ReadFields()
For intcolumn = 0 To 3
'store split lines in array
arrTableInfo(intFirstRec, intcolumn) = FieldString(intcolumn)
Next
Next
Catch
Dim arrTableInfo1(,) As String = {{"Year", "Box Office", "Title"}, {"1962", "$425,488,741 ", "Dr. No"}, {"1963", "$555,909,803 ", "From Russia with Love"}, {"1964", "$868,659,354 ", "Goldfinger"}, {"1965", "$966,435,555 ", "Thunderball"}, {"1967", "$720,388,023", "You Only Live Twice"}, {"1969", "$513,445,231 ", "On Her Majesty's Secret Service"}, {"1971", "$617,520,987 ", "Diamonds Are Forever"}, {"1973", "$785,677,477 ", "Live and Let Die"}, {"1974", "$426,826,774 ", "The Man with the Golden Gun"}, {"1977", "$666,367,656 ", "The Spy Who Loved Me"}, {"1979", "$624,527,272 ", "Moonraker"}, {"1981", "$481,005,579 ", "For Your Eyes Only"}, {"1983", "$405,873,493 ", "Octo*****"}, {"1985", "$316,186,616 ", "A View to a Kill"}, {"1987", "$362,876,056 ", "The Living Daylights"}, {"1989", "$271,586,451", "Licence to Kill"}, {"1995", "$499,954,330", "GoldenEye"}, {"1997", "$465,588,535 ", "Tomorrow Never Dies"}, {"1999", "$504,705,882 ", "The World Is Not Enough"}, {"2002", "$546,490,272 ", "Die Another Day"}, {"2006", "$640,803,677 ", "Casino Royale"}, {"2008", "$586,090,727 ", "Quantum of Solace"}}
For intFirstRec = 0 To UBound(arrTableInfo1)
For intcolumn = 0 To 3
'store split lines in array
arrTableInfo(intFirstRec, intcolumn) = arrTableInfo1(intFirstRec, intcolumn)
Next
Next
End Try
End Sub
Function GetFileLineCountNew(ByVal strFileName As String) As Integer
Dim intCounter As Integer
Dim strLine As String
Dim fh As Integer
'use free # for file opening
fh = FreeFile()
'set line count to 0
intCounter = 0
'open file
FileOpen(fh, strFileName, OpenMode.Input)
'use loop to run through file and count each line
Do Until EOF(fh)
strLine = LineInput(fh)
intCounter = intCounter + 1
Loop
FileClose(fh)
GetFileLineCountNew = intCounter
End Function
Private Sub cmbAgents_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Agentcmb.SelectedIndexChanged
'Clear text box before getting values
Histlist.Text = ""
'-1 signifies no selection made in dropdown
If Agentcmb.SelectedIndex <> -1 Then
'select Agent to Display
Select Case Agentcmb.SelectedIndex
'Use GetActorSuite function to gather info to display in Text area
Case 0
Histlist.Text = GetActorSuite(Agentcmb.Text)
Case 1
Histlist.Text = GetActorSuite(Agentcmb.Text)
Case 2
Histlist.Text = GetActorSuite(Agentcmb.Text)
Case 3
Histlist.Text = GetActorSuite(Agentcmb.Text)
Case 4
Histlist.Text = GetActorSuite(Agentcmb.Text)
Case 5
Histlist.Text = GetActorSuite(Agentcmb.Text)
Case Else
' No selection made -Clear data
Histlist.Text = ""
End Select
Else
Histlist.Text = ""
End If
End Sub
Function GetActorSuite(ByVal strActor As String) As String
'Declare vairables for Finding all information for specified agent
Dim intFirstEntry As Integer
Dim strFullLine As String
'Store info in a single variable to be displayed at the end
strFullLine = "Year Total Box Office Title" & vbNewLine
strFullLine = strFullLine & " ------- ----------------- --------------------------------- " & vbNewLine
For intFirstEntry = 0 To UBound(arrTableInfo)
'seach array until Agent's name matches
If arrTableInfo(intFirstEntry, 1) = Agentcmb.Text Then
For intFillArray = 0 To 3
If intFillArray = 2 Then
strFullLine = strFullLine & arrTableInfo(intFirstEntry, intFillArray) & vbTab & vbTab & vbTab
Else
strFullLine = strFullLine & arrTableInfo(intFirstEntry, intFillArray) & vbTab
End If
Next
strFullLine = strFullLine & vbNewLine
End If
Next
GetActorSuite = strFullLine
End Function
Private Sub Clearbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clearbtn.Click
Agentcmb.SelectedIndex = -1
Histlist.Text = ""
Avglbl.Text = ""
SeanCchk.Checked = False
GeorgeLchk.Checked = False
RogerMchk.Checked = False
TimDchk.Checked = False
PierceBchk.Checked = False
DanCchk.Checked = False
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()
End Sub
Private Sub InformationToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InformationToolStripMenuItem.Click
MsgBox("This program displays the Box Office totals for 007 Agents. In the Work History Area, you can select one of the Agents from the dropdown list to see all Missions and the box office amounts. In the Summary area, you may select one or more check boxes to see the highest average box office take and the Agent the average is associated with.")
End Sub
Private Sub SeanCchk_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SeanCchk.CheckedChanged
'Sean Connery - If checked, Find Average, if multiple Agents selected show highest avg.
If SeanCchk.Checked Then
Avglbl.Text = SeanCchk.Text & " " & Format(GetAvgByActor(SeanCchk.Text), "$0,000.00")
decSC = GetAvgByActor(SeanCchk.Text)
Else
'Set average to 0 for purposes of Who is higher array
decSC = 0
End If
'Store data in array to find who is higher (if multiple agents selected)
arrHighLow(0, 0) = SeanCchk.Text
arrHighLow(0, 1) = decSC.ToString
'Use sub/function to compare array values until Highest value is found -then display in label
GetMaxAverage(decSC)
End Sub
Private Sub GeorgeLchk_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GeorgeLchk.CheckedChanged
'George Lazenby - If checked, Find Average,if multiple Agents are selected show highest avg.
If GeorgeLchk.Checked Then
Avglbl.Text = GeorgeLchk.Text & " " & Format(GetAvgByActor(GeorgeLchk.Text), "$0,000.00")
decGL = GetAvgByActor(GeorgeLchk.Text)
Else
decGL = 0
End If
'Store data in array to find who is higher (if multiple agents selected)
arrHighLow(1, 0) = GeorgeLchk.Text
arrHighLow(1, 1) = decGL.ToString
'Use sub/function to compare array values until Highest value is found -display in label
GetMaxAverage(decGL)
End Sub
Private Sub RogerMchk_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RogerMchk.CheckedChanged
'Roger Moore - If checked, Find Average, multiple Agents are selected show highest avg.
If RogerMchk.Checked Then
Avglbl.Text = RogerMchk.Text & " " & Format(GetAvgByActor(RogerMchk.Text), "$0,000.00")
decRM = GetAvgByActor(RogerMchk.Text)
Else
decRM = 0
End If
'Store data in array to find who is higher (if multiple agents selected)
arrHighLow(2, 0) = RogerMchk.Text
arrHighLow(2, 1) = decRM.ToString
'Use sub/function to compare array values until Highest value is found -display in label
GetMaxAverage(decRM)
End Sub
Private Sub TimDchk_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimDchk.CheckedChanged
'Timothy Dalton - If checked, Find Average, if multiple Agents are selected show highest avg.
If TimDchk.Checked Then
Avglbl.Text = TimDchk.Text & " " & Format(GetAvgByActor(TimDchk.Text), "$0,000.00")
decTD = GetAvgByActor(TimDchk.Text)
Else
decTD = 0
End If
'Store data in array to find who is higher (if multiple agents selected)
arrHighLow(3, 0) = TimDchk.Text
arrHighLow(3, 1) = decTD.ToString
'Use sub/function to compare array values until Highest value is found -display in label
GetMaxAverage(decTD)
End Sub
Private Sub PierceBchk_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PierceBchk.CheckedChanged
'Pierce Brosnan - If checked, Find Average, if multiple Agents are selected show highest avg.
If PierceBchk.Checked Then
Avglbl.Text = PierceBchk.Text & " " & Format(GetAvgByActor(PierceBchk.Text), "$0,000.00")
decPB = GetAvgByActor(PierceBchk.Text)
Else
decPB = 0
End If
'Store data in array to find who is higher (if multiple agents selected)
arrHighLow(4, 0) = PierceBchk.Text
arrHighLow(4, 1) = decPB.ToString
'Use sub/function to compare array values until Highest value is found -display in label
GetMaxAverage(decPB)
End Sub
Private Sub DanCchk_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DanCchk.CheckedChanged
'Daniel Craig - If checked, Find Average, if multiple Agents are selected show highest avg.
If DanCchk.Checked Then
Avglbl.Text = DanCchk.Text & " " & Format(GetAvgByActor(DanCchk.Text), "$613,447,202.00")
decDC = GetAvgByActor(DanCchk.Text)
Else
decDC = 0
End If
'Store data in array to find who is higher (if multiple agents selected)
arrHighLow(5, 0) = DanCchk.Text
arrHighLow(5, 1) = decDC.ToString
'Use sub/function to compare array values until Highest value is found -display in label
GetMaxAverage(decDC)
End Sub
Private Sub GetMaxAverage(ByVal decValue As Decimal)
'declare variables for finding Highest avg
Dim intActorPlaceHolder As Integer
Dim strLabelText As String
strLabelText = ""
'compare value of current check box against each value within array (of all agents)
For intActorPlaceHolder = 0 To 5
If arrHighlow(intActorPlaceHolder, 1) > decValue.ToString Then
'If current position in array is higher
'Store Box office amount
decValue = CDec(arrHighlow(intActorPlaceHolder, 1))
strLabelText = arrHighlow(intActorPlaceHolder, 0) & " " & CDec(arrHighlow(intActorPlaceHolder, 1)).ToString("C")
ElseIf decValue <> 0 And decValue = CDec(arrHighlow(intActorPlaceHolder, 1)) Then
strLabelText = arrHighlow(intActorPlaceHolder, 0) & " " & CDec(arrHighlow(intActorPlaceHolder, 1)).ToString("C")
End If
Next
'display Agent name and Avg
Avglbl.Text = strLabelText
End Sub
Function GetAvgByActor(ByVal strActor As String) As Decimal
'Declare variables for finding the avg box office amount for selected agent(s)
Dim intFirstEntry As Integer
Dim decTotal As Decimal
Dim decAverage As Decimal
Dim intCounter As Integer
For intFirstEntry = 0 To UBound(arrTableInfo)
If arrTableInfo(intFirstEntry, 1) = strActor Then
'calc. total amount of all movies
decTotal = decTotal + CDec(arrTableInfo(intFirstEntry, 2))
'keep track of # of movies
intCounter = intCounter + 1
End If
Next
'calc. avg by dividing total of all amounts by # of movies
decAverage = decTotal / intCounter
'store and return avg
GetAvgByActor = decAverage
End Function
Private Function App_Path() As String
Throw New NotImplementedException
End Function
Private Function Delimited() As Object
Throw New NotImplementedException
End Function
End Class