I did see some other posts on this project, but nothing seemed to help. I just keep getting more confused. =P I coded all my issues into the comments, as this project is already late, and I'm going to have to hand it in - whether it works or not - tomorrow. I'm hoping someone can see something obvious that I'm doing incorrectly, so I don't lose so many points. =P

The assignment is to write a program to perform the following:
1. Display the info in the file csvSTOCKS.TXT. (Doesn't work - see btnDisplay in my coding)
2. Add an additional stock onto the end of csvSTOCKS.TXT. Input will be taken from text boxes. (see btnAdd in my coding - WORKS!)
3. Update the current price of a stock in csvSTOCKS.TXT. The file csvSTOCKS.TXT should be copied to TEMP.TXT until the named stock is found. It should then write the changed stock, and complete writing the rest of the original stocks. csvSTOCKS.TXT must then be deleted and TEMP.TXT renamed to csvSTOCKS.TXT. (Doesn't work - see btnUpdate and ChangeStock() sub) *cough* *choke* *gag*
4. Show a profit/loss statement. Pretty self-explanatory. (Doesn't work - see btnShow in my coding)
5. Quit - I got this! LOL

Imports System.IO

Public Class Form1
    'Define structure
    Structure StockData
        Dim stockName As String
        Dim shares As Integer
        Dim pDate As String
        Dim PP As Double
        Dim CP As Double
    End Structure

    'Declare class-level variables - Yes, I know I used these in my structure, but that's 
    'probably one of the issues affecting everything else?  I tried doing "Dim StockArray() 
    'As StockData, but I can't get it to work that way, either
    Dim stockName As String
    Dim shares As Integer
    Dim pDate As String
    Dim PP As Double
    Dim CP As Double

    'This button should display the information in the stock file.  Doesn't work.. Error - Make
    'sure list index on a list is less than the list size at "Dim line1 as string..."  Assuming
    'my array is set up incorrectly, but *ugh* I don't know how else to do it!
    Private Sub btnDisplay_Click(ByVal sender As System.Object, _
                                 ByVal e As System.EventArgs) Handles btnDisplay.Click

        'Format zones
        Dim fmtStr As String = "{0, -18}{1, -10} {2, -12} {3, -14} {4, -14}"
        Dim fmtStr1 As String = "{0, -18} {1, 10:N0} {2, 12:D} {3, 14:C2} {4, 14:C2}"

        'Again, an array is not a string, I know, but I'm not sure how else to 
        'initialize my array
        Dim stockArray() As String = File.ReadAllLines("csvSTOCKS.TXT")

        lstResults.Items.Clear()

        lstResults.Items.Add(String.Format(fmtStr, "", "Number", "Date", "Purchase", "Current")) 'my top labels being formatted by zone.
        lstResults.Items.Add(String.Format(fmtStr, "Stock", "of Shares", "Purchased", "Price/Share", "Price/Share")) 'my second line of labels being formatted.
        lstResults.Items.Add("_______________________________________________________________")

        'Declare and initialize counter variable
        Dim count As Integer
        count = stockArray.GetUpperBound(0)

        For i As Integer = 0 To count
            Dim csvData = (stockArray(i).Split(","c)) 'Split array into columns of data 

            'Assign format to each string of data
            Dim line1 As String = (String.Format(fmtStr1, csvData(0), csvData(1), csvData(2), csvData(3), csvData(4)))

            'Add line of data to the output
            lstResults.Items.Add(line1)
        Next
    End Sub

    'This button adds a new record to the file.  This one works....
    Private Sub btnAdd_Click(ByVal sender As System.Object, _
                             ByVal e As System.EventArgs) Handles btnAdd.Click


        'Ensure all input fields are filled
        If (txtName.Text <> "") And (numCurrPrice.Text <> "") And (numShares.Text <> "") And _
        (txtDate.Text <> "") And (numPurchPrice.Text <> "") Then
            Dim sw As StreamWriter = File.AppendText("csvSTOCKS.TXT")

            'Declare and initialize variables
            Dim stock As String = txtName.Text
            Dim shares As String = numShares.Text
            Dim CP As String = numCurrPrice.Text
            Dim pDate As String = txtDate.Text
            Dim PP As String = numPurchPrice.Text
            Dim newLine As String

            'Assign entry format for new record
            newLine = (stock & "," & shares & "," & pDate & "," & CP & "," & PP)

            'Write new line to file
            sw.WriteLine(newLine)

            sw.Close() 'Close file

            'Clear textboxes
            txtName.Clear()
            numCurrPrice.Clear()
            numShares.Clear()
            txtDate.Clear()
            numPurchPrice.Clear()
            txtName.Focus()

            'Display message if successful
            MessageBox.Show("Information has been added to the file.", "Success!")

        Else 'Display message if not successful
            MessageBox.Show("Check input fields for missing information.", "Input Incomplete")

        End If

    End Sub

    'This button should update a record in the file.  Doesn't work, guessing because it's
    'dependant on the ChangeStock() sub routine
    Private Sub btnUpdate_Click(ByVal sender As System.Object, _
                                ByVal e As System.EventArgs) Handles btnUpdate.Click

        'Declare variable for success/error messages
        Dim message As String

        'Ensure all input fields are filled
        If (txtName.Text <> "") And (numShares.Text <> "") _
        And (txtDate.Text <> "") And (numPurchPrice.Text <> "") _
        And (numCurrPrice.Text <> "") Then

            If File.Exists("csvSTOCKS.TXT") Then 'Check if file exists

                ChangeStock() 'Call sub to change stock data

            Else 'Display message if file does not exist
                message = "Either no file has yet been created or is not where expected."
                MessageBox.Show(message, "File Not Found.")

            End If

        Else ' Display message if a textbox is empty.
            MessageBox.Show("You must enter a stock.", "Incomplete Information")
            txtName.Focus() 'Place cursor in the stock name textbox
        End If



    End Sub

    'Sub procedure for changing stock data...  The loop is infinite.  It does create the
    'temp.txt file, but doesn't write correctly.  At last debugging run, the temp.txt file
    'creates a LSV, not a CSV, and simply repeats the stock name over and over and over... =P
    Sub ChangeStock()

        'Declare flag variable
        Dim foundflag As Boolean = False

        Dim sr As StreamReader = File.OpenText("csvSTOCKS.TXT")
        Dim sw As StreamWriter = File.CreateText("TEMP.TXT")

        Do While (sr.Peek <> -1)
            stockName = txtName.Text
            shares = numShares.Text
            pDate = txtDate.Text
            PP = numPurchPrice.Text
            CP = numCurrPrice.Text

            'If any field matches, update all fields
            If (stockName = txtName.Text) Or (shares = numShares.Text) _
            Or (pDate = txtDate.Text) Or (PP = numPurchPrice.Text) _
            Or (CP = numCurrPrice.Text) Then
                sw.WriteLine(stockName, shares, pDate, PP, CP)
            Else
                foundflag = True
            End If
        Loop
        sr.Close() 'Close reader
        sw.Close() 'Close writer

        File.Delete("csvSTOCKS.TXT") 'Delete original file
        File.Move("TEMP.TXT", "csvSTOCKS.TXT") 'Rename new file to original file name
        MessageBox.Show("The record has been updated.", "Success!") 'Confirm update was successful

        If Not foundflag Then 'Display message if update was not successful
            MessageBox.Show("The record was not found.", "Update Failed")

        Else 'Clear textboxes
            txtName.Clear()
            numShares.Clear()
            txtDate.Clear()
            numPurchPrice.Clear()
            numCurrPrice.Clear()
        End If
    End Sub


    'This should display a Profit/Loss statement.  Doesn't work.  Thinking it's AGAIN, my 
    'inability to properly declare, split, and initialize my array values.  =P
    Private Sub btnshow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnShow.Click
        lstResults.Items.Clear()
        Dim column(0 To 4) As String
        Dim row(20) As stockData
        Dim Cost, PL, CV As Double
        Dim fmtstr As String = "{0,-14}  {1,16}  {2,14}  {3,14}"
        lstResults.Items.Add(String.Format(fmtstr, "", "", "Current", "Profit"))
        lstResults.Items.Add(String.Format(fmtstr, "Stock", "Cost", "Value", "(or Loss)"))
        lstResults.Items.Add(String.Format(fmtstr, "==============", "================", "==============", "=============="))
        Dim i As Integer = 0
        Dim sr As StreamReader = New StreamReader("csvSTOCKS.TXT")
        Do While sr.Peek <> -1
            column = sr.ReadLine.Split(",")
            row(i).stockName = column(0)
            row(i).shares = CDbl(column(1))
            row(i).pDate = column(2)
            row(i).PP = CDbl(column(3))
            row(i).CP = CDbl(column(4))
            Cost = column(1) * column(3)
            CV = (column(1)) * (column(4))
            PL = CV - Cost
            lstResults.Items.Add(String.Format(fmtstr, row(i).stockName, Cost, CV, PL))
        Loop
        sr.Close()
    End Sub

    'Closes the application.  Works.
    Private Sub btnExit_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

End Class

Note that I tried to get help from my professor, and what he sent me did not include anything about an array or structures. I don't get it. I appreciate any insight that anyone can give. =)

Upload your project with the .CSV file inside of the zip. That is a lot of code to debug without it :)

Got it all now, except the Update sub and the ChangeStock sub. Anyway, I turned it in.

This question has already been answered. Start a new discussion instead.