Opening a file and loading it into an array in Visual Basic

This was for POS/408, done in Visual Basic .Net

' Project: Program 4
' Date: June 20, 2010
' Version: 1.03
' Release Notes: This program loads two text files using the OpenFileDialog and merges the contents together in one text
'                box. The user can choose to sort the list alphabetically or by box office total. The top 1/3 of the
'                list items will be displayed according to box office earnings.
'
' Requirements:
'                i. A text file loaded with title information
'               ii. A text file loaded with box office information
'
' Changes from version 1:
'                   i. Fixed the TopResultsLabel to display correct top one-third listings
'                  ii. Fix the sort buttons to sort without distorting relationship between arrays
'                 iii. Fixed Instructions to remove spelling errors
'                  iv. Added comments to the code.

' Import required files
Imports Microsoft.VisualBasic.FileIO
Imports System.IO

Public Class Program4

 ' Initialize variables
 Private TitleSortedArray() As String
 Private BoxOfficeSortedArray() As String
 Private TitleTextFieldParser As TextFieldParser
 Private BoxOfficeTextFieldParser As TextFieldParser
 Private TitleStreamReader As StreamReader
 Private BoxOfficeStreamReader As StreamReader

 ' When the "Load" button is clicked, the program will prompt the user to locate the file with title information,
 ' then load the information into an array. Immediately after the information is loaded, the user is prompted to locate
 ' the file containing the box office information. This information is then loaded into an array. Once both arrays
 ' have been loaded, the contents will be displayed in the text area.
 Private Sub LoadButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadButton.Click

 ' Initialize variables
 Dim ForCount As Integer
 Dim MaxSize As Integer = 0
 Dim FileName As String = ""
 Dim TitleArray() As String
 Dim BoxOfficeArray() As String
 Dim ResponseDialogResult As DialogResult

 ' Set up and display the Open File dialog.
 With OpenFileDialog1
 ' Filter the displayed files to only show files with .txt extensions.
 .Filter = "Text files (*.txt)|*.txt|" & "All files|*.*"
 ' Start the file search in the current directory
 .InitialDirectory = Directory.GetCurrentDirectory
 ' Display the default file name
 .FileName = "*.txt"
 ' Set the title for the window
 .Title = "Select File for Title Information"
 ' Display the Open File dialog box.
 ResponseDialogResult = .ShowDialog()
 End With

 ' If the "Cancel" button wasn't clicked...
 If ResponseDialogResult <> DialogResult.Cancel Then
 ' Set the variable to the selected path and file name
 FileName = OpenFileDialog1.FileName
 ' Read the stream from the open file
 TitleStreamReader = New StreamReader(OpenFileDialog1.OpenFile)

 ' Try to parse the text file
 ' Set the file type to delimited
 ' Specify the delimiting character
 ' Catch any errors and display an error message
 Try
 TitleTextFieldParser = New TextFieldParser(FileName)
 TitleTextFieldParser.TextFieldType = FieldType.Delimited
 TitleTextFieldParser.SetDelimiters(",")
 Catch
 MessageBox.Show("Unable to read the file: " & FileName, "File Error")
 End Try

 ' Try to execute the following code.
 ' Catch any errors and display error message
 Try
 ' If not the end of data...
 If Not TitleTextFieldParser.EndOfData Then
 ' Fill the TitleArray() with the contents of the opened file
 TitleArray = File.ReadAllLines(FileName)
 ' Set the TitleSortedArray() to the same contents as the TitleArray for later sorting
 TitleSortedArray = TitleArray
 ' If end of data....
 Else
 ' ...display an error message
 MessageBox.Show("No more records to display.", "End of Data")
 End If

 ' The following code executes automatically after the title file has been selected.
 ' This code WILL NOT execute if the user clicks the "Cancel" button when searching for title text file

 ' Set up and display the Open File dialog.
 With OpenFileDialog1
 ' Filter the results to only show files with .txt extension
 .Filter = "Text files (*.txt)|*.txt|" & "All files|*.*"
 ' Start the file search in the initial directory
 .InitialDirectory = Directory.GetCurrentDirectory
 ' Set the default file name
 .FileName = "*.txt"
 ' Set the title for the window
 .Title = "Select File for Box Office Information"
 ' Display the Open File dialog box.
 ResponseDialogResult = .ShowDialog()
 End With

 ' Make sure that the user didn’t click the Cancel button.
 If ResponseDialogResult <> DialogResult.Cancel Then

 ' Set the variable to the selected patch and file name
 FileName = OpenFileDialog1.FileName
 ' Set the StreamReader to read the contents of the open file
 BoxOfficeStreamReader = New StreamReader(OpenFileDialog1.OpenFile)

 ' Try to parse the contents of the text file
 ' Set the field type to delimited
 ' Specify the delimiter character
 ' Catch any errors and display an error message
 Try
 BoxOfficeTextFieldParser = New TextFieldParser(FileName)
 BoxOfficeTextFieldParser.TextFieldType = FieldType.Delimited
 BoxOfficeTextFieldParser.SetDelimiters(",")
 Catch
 MessageBox.Show("Unable to read the file: " & FileName, "File Error")
 End Try

 ' If not the end of data...
 If Not BoxOfficeTextFieldParser.EndOfData Then
 ' Read all file data and store in the BoxOfficeArray()
 BoxOfficeArray = File.ReadAllLines(FileName)
 ' Set the contents of the BoxOfficeSortedArray to the same as BoxOfficeArray for later sorting
 BoxOfficeSortedArray = BoxOfficeArray
 ' If end of data...
 Else
 ' ...display an error message
 MessageBox.Show("No more records to display.", "End of Data")
 End If

 ' Try to execute the following code
 Try

 ' Clear the TitleTextArea
 TitleTextArea.Text = ""
 ' For loop displays all contents of TitleArray() and BoxOfficeArray() in the text area
 ' ForCount increments to set index of array
 For ForCount = 0 To TitleArray.Length - 1
 ' Displays all contents of TitleArray() and BoxOfficeArray() in the text area
 TitleTextArea.Text += TitleArray(ForCount) & vbTab & FormatCurrency(BoxOfficeArray(ForCount)) & _
 vbNewLine
 ' Increase ForCount
 Next ForCount
 ' Catch any errors and display error message
 Catch
 MessageBox.Show("Cannot Format Non-Number Values Into Currency!")
 End Try

 ' If there are 3 or more items in the array, display the first 1/3 on a label
 If TitleSortedArray.Length >= 3 Then
 ' Set the label text to receive information
 TopResultsLabel.Text = "Top One-Third" & vbNewLine
 ' Reverse the sort to display items in descending order
 Array.Sort(BoxOfficeSortedArray, TitleSortedArray)

 ' Set the ForCount to loop through 1/3 of the array's contents
 For ForCount = (TitleSortedArray.Length - 1) To (TitleSortedArray.Length - _
 (TitleSortedArray.Length / 3)) Step -1
 ' Display the information on the label
 TopResultsLabel.Text += TitleSortedArray(ForCount) & " " & _
 FormatCurrency(BoxOfficeSortedArray(ForCount)) & vbNewLine
 ' Increase the ForCount
 Next ForCount
 End If
 End If

 ' Catch any errors and display an error message
 Catch
 MessageBox.Show("No file was selected!")
 End Try
 End If
 End Sub

 ' Sorts the displayed list in descending alphabetical order
 Private Sub SortTitleButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SortTitleButton.Click

 ' Sort the array alphabetically using the first array as an index key to maintain relationship between both arrays
 Array.Sort(TitleSortedArray, BoxOfficeSortedArray)

 ' Try to display the contents of the arrays
 Try
 ' Reset the TitleTextArea
 TitleTextArea.Text = ""
 ' Loop through all items in the array
 For ForCount = 0 To TitleSortedArray.Length - 1
 ' Display the sorted contents of the array
 TitleTextArea.Text += TitleSortedArray(ForCount) & vbTab & FormatCurrency(BoxOfficeSortedArray(ForCount)) & _
 vbNewLine
 ' Increase the ForCount
 Next ForCount
 ' Catch any errors and display an error message
 Catch
 MessageBox.Show("Cannot Format Non-Number Values Into Currency!")
 End Try

 End Sub

 ' Sort the displayed list in descending numerical order
 Private Sub SortBoxOfficeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SortBoxOfficeButton.Click

 ' Sort the array numerically using the first array as an index key to maintain the relationship between both arrays
 Array.Sort(BoxOfficeSortedArray, TitleSortedArray)

 ' Try to display the contents of the arrays
 Try
 ' Reset the TitleTextArea
 TitleTextArea.Text = ""
 ' Loop through all items in the array
 For ForCount = 0 To TitleSortedArray.Length - 1
 ' Display the sorted contents of the array
 TitleTextArea.Text += TitleSortedArray(ForCount) & vbTab & FormatCurrency(BoxOfficeSortedArray(ForCount)) & _
 vbNewLine
 ' Increase the ForCount
 Next ForCount
 ' Catch any errors and display an error message
 Catch
 MessageBox.Show("Cannot Format Non-Number Values Into Currency!")
 End Try
 End Sub

 ' Clear the TitleTextArea, the TopResultsLabel, and all arrays
 Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click

 ' Clear the text area
 TitleTextArea.Text = ""
 ' Clear the label
 TopResultsLabel.Text = "Top One-Third:" & vbNewLine
 ' Clear the array
 Array.Clear(TitleSortedArray, 0, TitleSortedArray.Length)
 ' Clear the array
 Array.Clear(BoxOfficeSortedArray, 0, BoxOfficeSortedArray.Length)

 End Sub

 ' Display instructions when the Instructions option in the Help menu is clicked
 Private Sub InstructionsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InstructionsToolStripMenuItem.Click

 ' Display instructions to the user
 MessageBox.Show("Click ""Load Data""" & vbNewLine & "Select the file containing title data" & vbNewLine & _
 "Select the file containing box office data" & vbNewLine & "Click ""Sort Title"" to sort the list " & _
 "by title name" & vbNewLine & "Click ""Sort Box Office"" to sort the list by box office amount" & _
 vbNewLine & "Top one-third missions will also be displayed according to box office")
 End Sub

 ' Exit the program when the Exit option is clicked in the File Menu
 Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click

 ' Close the program
 Me.Close()
 End Sub
End Class

© 2010, Within this mind. All rights reserved.

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • Blogosphere
  • Fark
  • Google Buzz
  • LinkedIn
  • Reddit
  • Slashdot
  • Suggest to Techmeme via Twitter

About Kinda Strange

I am a student at the University of Phoenix majoring in information technology. This is where I come to babble incoherently…err…make notes, talk about things that catch my interest, share ideas, etc...
This entry was posted in Homework and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>